在collectionview中选择和取消选择行时出错

时间:2016-07-01 10:11:40

标签: ios swift uicollectionview

我在collectionView中快速选择取消选择行时遇到问题。 我有一张地图,在它上面我有一个水平collectionView,我选择了我想要看的东西。

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        let selectedCell:UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!
        selectedCell.contentView.backgroundColor = UIColor(red: 102/256, green: 255/256, blue: 255/256, alpha: 0.66)
        switch indexPath.row {
        case 0:
            query0()
            break
        case 1:
            query1()
            break
        case 2:
            query2()
            break
        case 3:
            query3()
            break
        default:

            break
        }

    }

和取消选择的代码是:

 func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
        let cellToDeselect:UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!
        cellToDeselect.contentView.backgroundColor = UIColor.clearColor()
    }

我得到的错误是:

  

致命错误:在解包可选值时意外发现nil

当我试图慢慢地选择细胞时,我没有得到错误 但如果我快速崩溃它会崩溃

我评论了deselect功能,我没有得到任何错误(我通过快速更换单元格检查它)

4 个答案:

答案 0 :(得分:4)

而不是改变didSelectdidDeSelect中的颜色,尝试这样的事情

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell
     let view = UIView(frame: cell.contentView.bounds)
     view.backgroundColor = UIColor(red: 102/256, green: 255/256, blue: 255/256, alpha: 0.66)
     cell.selectedBackgroundView = view
}

希望这会对你有所帮助。

答案 1 :(得分:2)

您的应用程序崩溃因为当您使用

访问隐藏单元格时,您的nil为nil
 let cellToDeselect:UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!

将cellToDeselect变量作为可选&在改变颜色的同时使用可选链接。

用此

替换您的代码
let cellToDeselect:UICollectionViewCell? = collectionView.cellForItemAtIndexPath(indexPath)?
        cellToDeselect?.contentView.backgroundColor = UIColor.clearColor()

答案 2 :(得分:2)

不要强行展开$.each($(".tab"), function(index) { element_id = $(this).attr('id').slice(-1); if ( $('.section'+ element_id ).length ){ html_text = $(".section"+element_id).html(); $(this).html('<span class="section">'+html_text+'</span>'); } }); 的结果。您可以使用<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span class="section2 section4">hello</span> <span class="section1 section6">World</span> <div class="tab" id="tab1"></div> <div class="tab" id="tab6"></div> <div class="tab" id="tab3"></div> <div class="tab" id="tab4"></div> <div class="tab" id="tab5"></div>声明:

cellForItemAtIndexPath

答案 3 :(得分:2)

正如其他人已经说过的那样,你正在获得nil,因为你强行打开一个因某些原因无法使用的单元格。强行展开是非常危险的,你应该尽量避免它。有多种方法可以做到这一点,你可以做Rohit KP所说的并使用可选的分配变量,或者你可以这样做(我更喜欢):

if let cellToDeselect = collectionView.cellForItemAtIndexPath(indexPath) as? UICollectionViewCell {
   cellToDeselect.contentView.backgroundColor = UIColor.clearColor()
}

这是按照您想要的方式分配变量,如果它没有返回nil值,它将进入if statement块并执行您的代码。我更喜欢这种方式的原因是因为它更容易阅读。您知道如果您的对象是nil则无法进入此闭包,并且您也不必强制解包或分配任何可选变量,因此您可以清楚地了解此代码的运行时间和你也知道这是安全的。