Swift 2-调用`didDeselectItemAtIndexPath`时发生致命错误 [英] Swift 2 - Fatal Error when `didDeselectItemAtIndexPath` is called

查看:148
本文介绍了Swift 2-调用`didDeselectItemAtIndexPath`时发生致命错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UICollectionView,其中使用函数didSelectItemAtIndexPath选择一个单元格并更改其alpha.

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

UICollectionView中有12个单元格.

为了将取消选择的单元格带回到alpha = 1.0,我使用了功能didDeselectItemAtIndexPath.

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

到目前为止,代码仍然有效,当我选择一个单元格并滚动UICollectionView时,应用程序在deselect函数内的let colorCell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!行崩溃,并显示错误:

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:

严重错误:解开可选值时意外发现nil (lldb)

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
    }

推荐答案

发生崩溃是因为您选择并滚动到屏幕可见区域之外的单元格已被集合视图中的其他单元格重用.现在,当您尝试使用cellForItemAtIndexPath提取didDeselectItemAtIndexPath中的所选单元格时,会导致崩溃.

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.

如@Michael Dautermann所述,为避免崩溃,请使用可选绑定来验证单元格是否为nil,然后设置alpha

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
    }
}

为了在滚动过程中保持选择状态,请检查单元格的选择状态,并在使用cellForItemAtIndexPath方法将单元格出队时相应地设置alpha

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
}

这篇关于Swift 2-调用`didDeselectItemAtIndexPath`时发生致命错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆