简体   繁体   English

UICollectionView和Supplementary View崩溃

[英]UICollectionView and Supplementary View crash

I have a UICollectioView that works fine. 我有一个UICollectioView工作正常。 There are 2 ways to get to this page. 有两种方法可以访问此页面。

  1. though the app. 虽然应用程序。
  2. tap on push notification. 点按推送通知。

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. 如果用户在聊天中,然后他退出应用程序(主页按钮),那么他会得到推送通知,他按下它并且应用程序崩溃。

crash: 崩溃:

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 2015-09-25 10:28:15.140 Quest [298:16922] *断言失败 - [UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:],/ BuildRoot / Library / Cache / 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的未捕获的异常终止

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 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
    }
}

If you are using custom layout in your collection view, check if its datasource is nil in: 如果您在集合视图中使用自定义布局,请检查其数据源是否为零:

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect

The result should look something like this: 结果应如下所示:

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

This change resolves the following issue: 此更改解决了以下问题:

  • Assertion failure in -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:] 断言失败 - [UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:]

You can also check the following topics, however they didn't resolve anything in my case: UICollectionView and Supplementary View (header) 您还可以检查以下主题,但在我的情况下它们没有解决任何问题: UICollectionView和Supplementary View(标题)

Removing empty space, if the section header is hidden in the UICollectionView 如果节标题隐藏在UICollectionView中,则删除空白空间

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. @eeeee谢谢你指点我正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM