2016-07-01 47 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,你不能輸入這個閉包,而且你也不必強制解包或分配任何可選變量,所以你清楚地瞭解了這段代碼的運行時間,並且你也知道它是安全的。