UICollectionView和补充视图崩溃

我有一个UICollectioView工作正常。 有2种方法可以到达这个页面。

  1. 虽然应用程序。
  2. 点击推送通知。

除了一种情况,一切都很好。

如果用户正在聊天,然后他退出应用程序(主页button),然后他得到一个推送通知他按下,应用程序崩溃。

崩溃:

2015-09-25 10:28:15.140 Quest [298:16922] *声明失败 – [UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:],/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/ UIKit-3505.16 / UICollectionView.m:1519 2015-09-25 10:28:15.146任务[298:16922] *由于未捕获exception'NSInternalInconsistencyException',原因:'UICollectionView dataSource未设置'***终止应用程序调用堆栈:(0x1828b4f5c 0x1973b7f80 0x1828b4e2c 0x1837a3f3c 0x187f97a38 0x187e6ebd4 0x187e6fb54 0x187e69b88 0x187e0700c 0x18760df14 0x187608b20 0x1876089e0 0x18760807c 0x187607dd0 0x1880c0bd4 0x18286c48c 0x18286bdc4 0x182869d4c 0x182798dc0 0x18d8ec088 0x187e72f60 0x100215590 0x197be28b8)的libc ++ abi.dylib:与typesNSException的未捕获的exception终止

推送处理器中的代码:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { let home = storyboard.instantiateViewControllerWithIdentifier("home") as! HomeViewController let chat = storyboard.instantiateViewControllerWithIdentifier("chat") as! ChatViewController var controllers : [UIViewController] = [login, chat] NotificationManager.handleNotification(userInfo, controllers: &controllers, storyboard: storyboard) navC.viewControllers = controllers self.window?.makeKeyAndVisible() } 

集合视图设置代码

 @IBOutlet weak var chatCollectionView: UICollectionView! { didSet { chatCollectionView.registerNib(UINib(nibName: "ChatTimeStampCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ChatTimeStampCell") chatCollectionView.registerNib(UINib(nibName: "ChatReceiverCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ChatReceiverCell") chatCollectionView.registerNib(UINib(nibName: "ChatSenderCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ChatSenderCell") chatCollectionView.registerNib(UINib(nibName: "ChatHeaderCollectionReusableView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "ChatHeaderCell") chatCollectionView.dataSource = self chatCollectionView.delegate = self } } 

如果您在集合视图中使用自定义布局,请检查其数据源是否在:

 - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect 

结果应该是这样的:

 - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect if (self.collectionView.dataSource != nil) { // Your layout code return array; } return nil; } 

此更改解决了以下问题:

  • 断言失败 – [UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:]

您也可以检查以下主题,但是在我的情况下,它们没有解决任何问题: UICollectionView和补充视图(标题)

如果部分标题隐藏在UICollectionView中,则删除空白空间

希望它能帮助你,而且不会像我那样浪费时间。 @eeeee谢谢你指出我在正确的方向。