2017-07-28 108 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() 
}