我正在使用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'将具有零值。请问有人如何解决这个问题。
答案 0 :(得分:0)
我认为这可能适合你。我在我的应用程序中对它进行了测试,但它确实有用。
let deselectedCell = collectionView.cellForItemAtIndexPath(indexPath) as CollectionCellForAlertView
deselectedCell.imageView.image = UIImage(named: "someOtherImage.png")