2017-07-28 61 views
0

我想要實現此功能:在我的應用程序,如果我選擇了一個UICollectionView單元格,然後邊框變成藍色,而如果我選擇另外一個,以前的應該取消選擇,邊界應該變得透明。還有就是我寫的方法:didDeselectItemAt indexPath不觸發

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ChatCell 

     /* Set some settings */ 
     if globalSelected[indexPath.item] { 
      cell.circleView.layer.borderColor = UIColor.blue.cgColor 
     } else { 
      cell.circleView.layer.borderColor = UIColor.clear.cgColor 
     } 

     return cell 
} 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    //Global variable for maintain selection 
    global.selectedChatPath = indexPath 
    globalSelected[indexPath.item] = true 
    collectionView.reloadData() 
} 

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) { 
    if indexPath != nilPath { 
     globalSelected[indexPath.item] = false 
     collectionView.reloadData() 
    } 
} 

nilPath只是IndexPath(項目:-1,部分:0),但不要緊,因爲collectionView(_ collectionView:UICollectionView,didDeselectItemAt indexPath:IndexPath)甚至沒有被調用。我的CollectionView有allowSelection =真allowsMultipleSelection =假性能。我會感謝任何幫助。

+0

附加委託的CollectionView 「collectionView.delegate =自我」 – Ragul

+0

@Ragul謝謝!但它已經完成了:我有另一個類,它符合UICollectionViewDataSource和UICollectionViewDelegate協議,並且我將這個類的一個實例聲明爲我的UICollectionView的委託和數據源。另外,第一步工作正常:點擊任何單元格後,邊框變成藍色(這意味着代表團可以正常工作),但是當我點擊其他單元格時,它們的邊框也會變成藍色,但以前的邊框細胞仍然是藍色的(我想透明)。 –

+0

只是重新加載collectionView每個選擇 – Ragul

回答

3

如果只有一個單元應該在同一時間,我建議把當前所選索引路徑成實例變量來選擇(nil意味着什麼被選中)

var selectedIndexPath : IndexPath? 

cellForItemAt設置爲根據實例變量

顏色
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ChatCell 

    /* Set some settings */ 
    if let selected = selectedIndexPath, selected == indexPath { 
     cell.circleView.layer.borderColor = UIColor.blue.cgColor 
    } else { 
     cell.circleView.layer.borderColor = UIColor.clear.cgColor 
    } 

    return cell 
} 

didSelectItemAt只重新載入前一個和新選定的單元格,並將selectedIndexPath設置爲新選定的索引路徑。這比重新加載整個集合視圖更有效。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    //Global variable for maintain selection 

    var cellsToReload = [indexPath] 
    if let selected = selectedIndexPath { 
     cellsToReload.append(selected) 
    } 
    selectedIndexPath = indexPath 
    collectionView.reloadItems(at: cellsToReload) 
} 

didDeselectItemAt僅當需要明確取消選擇單元格時才需要。

0
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! CollectionViewCell 
    if selectedIndex == indexPath.item { 
    cell.backgroundColor = .blue 
    } else { 
    cell.backgroundColor = .clear 
    } 
} 

和didSelectItemAt,

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    selectedIndex = indexPath.item 
    collectionView.reloadData() 
} 
+0

謝謝!我糾正了我的問題(現在它也包含cellForItemAt方法)。 –

+0

你的代碼和collectionview在同一個控制器上? – Ragul

1

試試這個

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ChatCell 

     /* Set some settings */ 
     if globalSelected[indexPath.item] { 
      cell.circleView.layer.borderColor = UIColor.blue.cgColor 
      collectionView.selectItemAtIndexPath(indexPath, animated: false, scrollPosition: .None) 
     } 
     else { 
      cell.circleView.layer.borderColor = UIColor.clear.cgColor 
      collectionView.deselectItemAtIndexPath(indexPath, animated: false) 
     } 
     return cell 
     } 
0

刷新您的UICollectionView每次一個單元被選中,然後更改所需單元格的邊框。 重新加載數據時,將刪除先前單元格的邊框,之後您可以將邊框添加到所需的單元格。

0

didDeselectItem不會被調用,直到你再次點擊選定的單元格。爲了取消先前選定單元格,當您點擊另一個單元格,你需要設置先前選定單元格的設置在全局變量falsedidSelectItem如下所示:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    globalSelected[global.selectedChatPath.item] = false //Set the previously selected cell's setting to false 

    //Global variable for maintain selection 
    global.selectedChatPath = indexPath 
    globalSelected[indexPath.item] = true 
    collectionView.reloadData() 
}