【发布时间】:2015-06-08 20:47:24
【问题描述】:
我正在开发 UICollectionView。当用户选择它时,我将一个图像分配给集合视图单元格,并在用户选择其他单元格时更改图像,如下代码所示
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
let selectedCell : CollectionCellForAlertView = collectionView.cellForItemAtIndexPath(indexPath) as! CollectionCellForAlertView
println("cell selected")
selectedCell.imageView.image = UIImage(named: "someImage.png")
}
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath){
println("deselected")
let deselectedCell : CollectionCellForAlertView = collectionView.cellForItemAtIndexPath(indexPath) as! CollectionCellForAlertView
deselectedCell.imageView.image = UIImage(named: "someOtherImage.png")
}
有时它可以正常工作,但有时它会崩溃,因为 'deselectedCell' 将具有 nil 值。谁能告诉我如何解决这个问题。
【问题讨论】:
标签: swift ios8 uicollectionview uicollectionviewcell