Swift 2 - Fatal Error when `didDeselectItemAtIndexPath` is called

丶灬走出姿态 提交于 2019-12-11 12:05:12

问题


I have a UICollectionView where I use the function didSelectItemAtIndexPath to select a cell and change its alpha.

In the UICollectionView there are 12 cells.

In Order to take the deselected cells back to alpha = 1.0 I use the function didDeselectItemAtIndexPath.

So far the code works however, when I select a cell and I scroll the UICollectionView the app crashes on the line let colorCell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)! inside the deselect function with the error:

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

I think I need to reload the collection view but How can I reload and keep the cell selected?

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
    }

回答1:


Crash happened because the cell that you selected and scrolled out of the visible region of the screen had been reused for other cells in the collection view. Now, when you try to fetch that selected cell in didDeselectItemAtIndexPath using cellForItemAtIndexPath, it resulted in a crash.

To avoid crash, as mentioned by @Michael Dautermann, use optional binding to validate if the cell is nil and then set the alpha

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
    if let cell = collectionView.cellForItemAtIndexPath(indexPath) {
        cell.alpha = 1.0
    }
}

In order to persist your selection state during scrolling, check the cell's selection state and set your alpha value accordingly when you dequeue your cell in cellForItemAtIndexPath method

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)

    if cell.selected {
        cell.alpha = 0.4
    }
    else {
        cell.alpha = 1.0
    }

    return cell
}



回答2:


cellForItemAtIndexPath seems to be returning an optional, so why not do:

override func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
    if let colorCell = collectionView.cellForItemAtIndexPath(indexPath) {
       colorCell.alpha = 1.0
    }
}


来源:https://stackoverflow.com/questions/33962068/swift-2-fatal-error-when-diddeselectitematindexpath-is-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!