Error selecting and deselecting collections

I have a problem when I quickly select and deselect rows in collectionView. I have a map, and above it I have a horizontal one collectionView, which I choose what I want to see.

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
        }

    }

and code to deselect:

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

The error I am getting is:

fatal error: nil unexpectedly found while deploying optional value

When I tried to select cells slowly, I didnโ€™t get an error But if I do it quickly, it will work

I commented on the deselection function and I did not receive any errors (and I check them with quick cell change)

+4
source share
4 answers

didSelect didDeSelect -

 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
}

, .

+4

. , ,

 let cellToDeselect:UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!

cellToDeselect .

let cellToDeselect:UICollectionViewCell? = collectionView.cellForItemAtIndexPath(indexPath)?
        cellToDeselect?.contentView.backgroundColor = UIColor.clearColor()
+2

cellForItemAtIndexPath. if let:

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

- , nil, , - . , . , , Rohit KP, , ( ):

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

This assigns the variable the way you want, and if it does not return a value nil, it will go into a block if statementand execute your code. The reason I like it so much is that it is easier to read. You know that you cannot enter this closure if your object nil, and you also do not need to force unwrap or assign any optional variables, so you have a clear idea of โ€‹โ€‹when this code works, and you also know that it is safe.

+2
source

All Articles