2016-07-01 46 views
4

我遇到问题时我快速选择取消选择行在我的collectionView中。 我有一张地图,上面有一个水平的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() 
    } 

错误我得到的是这样的:

fatal error: unexpectedly found nil while unwrapping an Optional value

,当我试图慢慢选择单元格,我没有得到错误 但是,如果我迅速做它崩溃

我评论的取消功能,我没有得到任何错误(我用细胞的快速变化检查)

+0

看看这个unswear http://stackoverflow.com/a/22861956/4525866 – salabaha

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

希望这会帮助你。

+0

这解决了错误和选择的两个单元格 –

+0

非常感谢你 –

+0

欢迎...... :) –

2

因为你得到零当您访问使用

let cellToDeselect:UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)! 
看不见的细胞您的应用程序崩溃

将cellToDeselect变量设置为可选&在更改颜色时使用可选链接。

与此

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

这解决了错误,但现在我有时选择了两个细胞 –

2

替换你的代码不要强行展开的cellForItemAtIndexPath结果。您可以使用if let声明:

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

这解决了错误,但有时2细胞被选中... –

+0

所以,你应该使用@Nirav – t4nhpt

+0

是的答案谢谢! –

2

正如其他人已经说过的,你会得到nil,因为你强制解开一个不能用于某种原因的单元。部队解包是相当危险的,你应该尽量避免它。有多种方法可以做到这一点,你既可以做什么罗希特KP表示,并使用可选的分配变量,或者你可以做这样的(我喜欢):

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

这是分配变量你想要的方式,如果它没有返回nil值,它将进入if statement块并执行你的代码。我喜欢这种方式更好的原因是因为它更易于阅读。你知道一个事实,如果你的对象是nil,你不能输入这个闭包,而且你也不必强制解包或分配任何可选变量,所以你清楚地了解了这段代码的运行时间,并且你也知道它是安全的。