UICollectionView 和补充视图崩溃 [英] UICollectionView and Supplementary View crash

查看:15
本文介绍了UICollectionView 和补充视图崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行良好的 UICollectioView.有两种方法可以访问此页面.

I have a UICollectioView that works fine. There are 2 ways to get to this page.

  1. 通过应用程序.
  2. 点击推送通知.

除了一个案例,一切都很好.

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:15192015-09-25 10:28:15.146 Quest[298:16922] * 终止应用程序由于未捕获的异常NSInternalInconsistencyException",原因:'未设置 UICollectionView 数据源'*** 第一次抛出调用堆栈:(0x1828b4f5c 0x1973b7f80 0x1828b4e2c 0x1837a3f3c 0x187f97a38 0x187e6ebd4 0x187e6fb54 0x187e69b880x187e0700c 0x18760df14 0x187608b20 0x1876089e0 0x18760807c0x187607dd0 0x1880c0bd4 0x18286c48c 0x18286bdc4 0x182869d4c0x182798dc0 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
    }
}

推荐答案

如果你在你的collection view中使用自定义布局,检查它的datasource是否为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:]

您还可以查看以下主题,但在我的情况下它们没有解决任何问题: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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆