【发布时间】:2015-11-27 17:10:08
【问题描述】:
我有一个UICollectionView,我使用函数didSelectItemAtIndexPath 来选择一个单元格并更改其alpha。
UICollectionView 中有 12 个单元格。
为了将取消选择的单元格带回alpha = 1.0,我使用函数didDeselectItemAtIndexPath。
到目前为止,代码仍然有效,当我选择一个单元格并滚动 UICollectionView 时,应用程序在取消选择函数内的 let colorCell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)! 行上崩溃并出现错误:
致命错误:在展开可选值时意外发现 nil (lldb)
我想我需要重新加载集合视图,但是如何重新加载并保持选中单元格?
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let colorCell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!
colorCell.alpha = 0.4
}
override func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
let colorCell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!
colorCell.alpha = 1.0
}
【问题讨论】:
标签: ios uicollectionview swift2 fatal-error didselectrowatindexpath