UICollectionView和补充视图崩溃 [英] UICollectionView and Supplementary View crash
问题描述
我有一个工作正常的UICollectioView. 有两种访问此页面的方法.
I have a UICollectioView that works fine. There are 2 ways to get to this page.
- 虽然是应用程序.
- 点击推送通知.
除了一个案例,一切都很好.
Everything works good except one case.
如果用户正在聊天,然后退出应用程序(主页按钮) 然后他会收到一个推送通知,按下该通知,应用就会崩溃.
if the user is in the chat and then he exits the app (home button) then he gets a push notification he presses it and the the app crashes.
崩溃:
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 Quest [298:16922] * 由于以下原因终止了应用 未捕获的异常"NSInternalInconsistencyException",原因: 未设置UICollectionView数据源" ***第一个抛出调用堆栈:(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:以类型未捕获的异常终止 NSException
2015-09-25 10:28:15.140 Quest[298:16922] * Assertion failure in -[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 Quest[298:16922] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView dataSource is not set' *** First throw call stack: (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: terminating with uncaught exception of type NSException
推送处理程序中的代码:
code in push handler:
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()
}
collectioview设置代码
collectioview setting code
@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
}
}
推荐答案
如果您在集合视图中使用自定义布局,请检查其数据源是否为nil in
If you are using custom layout in your collection view, check if its datasource is nil in:
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
结果应如下所示:
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
if (self.collectionView.dataSource != nil) {
// Your layout code
return array;
}
return nil;
}
此更改解决了以下问题:
This change resolves the following issue:
- -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:]中的断言失败
- Assertion failure in -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:]
您还可以检查以下主题,但是在我看来,它们没有解决任何问题: UICollectionView和补充视图(标题)
You can also check the following topics, however they didn't resolve anything in my case: UICollectionView and Supplementary View (header)
如果删除空白空间,如果该节标题隐藏在UICollectionView中
希望它能对您有所帮助,并且您不会像我那样浪费太多时间. @eeeee感谢您为我指出正确的方向.
Hope it will help you, and you won't waste as much time as I did. @eeeee thank you for pointing me in the right direction.
这篇关于UICollectionView和补充视图崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!