我只有一个简单的UIAlertController,我通过点击按钮显示它:
let alert = UIAlertController(title: "", message: NSLocalizedString("Are you sure you want to log out?", comment: ""), preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Sign Out", style: UIAlertActionStyle.default, handler: { (alert: UIAlertAction) in
self.dismiss(animated: true, completion: nil) // CRASH
}))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
我想用这个对话框动作解雇我的控制器 .
因此,当我点击警报中的“注销”按钮时,我的应用程序崩溃了 .
Crash log:
断言失败 - [UICollectionViewData validateLayoutInRect:],/ BuildRoot / Library / Cache / com.apple.xbs / Source / UIKit_Sim / UIKit-3698.33.6 / UICollectionViewData.m:435 2018-02-23 00:11:17.741531 0300 App [4681:1373962] *由于未捕获的异常终止应用'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist: {length = 2, path = 0 - 0}'
但!我根本没有CollectionView .
NOTE: 如果我只使用一个简单的self.dismiss(...)而没有这个警报动作,那么我的控制器就像正常一样被解雇了 .
NOTE 2: 我想解雇的控制器是一个SplitViewController,我没有任何CollectionViews .
NOTE 3: 我使用self.present以简单的方式呈现我的SplitViewController(splitVC,animated:true)
有什么建议?
1 回答
问题解决了 .
此崩溃是由layoutAttributesForElements引起的 . 我的缓存数组未被清除,当layoutAttributesForElements被调用时出现崩溃 .
如果您有自定义的CollectionView布局,则应始终清除UICollectionViewLayout的prepare()中的缓存 .
这就是问题所在 .