xcode UICollectionViewCell 快速选择多个单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31107856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:23:23  来源:igfitidea点击:

UICollectionViewCell Selecting multiple cells swift

xcodeswift

提问by Mohamed Horani

I'm trying to make an app to select multiple Cells, so let's assume we have 9 cells Indexed from 0 to 8.

我正在尝试制作一个应用程序来选择多个单元格,所以假设我们有 9 个单元格索引从 0 到 8。

Here is what i was trying to do...

这就是我想要做的......

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

var cell = collectionView.cellForItemAtIndexPath(indexPath)

   if cell?.selected == true {
    cell?.backgroundColor = UIColor.orangeColor()
   }

}

And Deselect Func()

并取消选择 Func()

    func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {        
       var cell = collectionView.cellForItemAtIndexPath(indexPath)
        cell?.backgroundColor = UIColor.clearColor()
    }

It's simple enough if a user selects a cell -> orange if deselects a cell -> clearColor 'white' So yea this code is working except

如果用户选择一个单元格 -> 如果取消选择一个单元格 -> 橙色 -> clearColor 'white' 所以是的,这段代码正在工作,除了

If a user selects cell 0, it also change the color of cell 6, "keep in mind cell 6 is not selected it just change it's appearance". Same for Cell 1 if it selected cell 7 changes and so on, also if i deselect either cell 0 or cell 6 both Changes.

如果用户选择单元格 0,它也会更改单元格 6 的颜色,“请记住,单元格 6 没有被选中,它只是改变了它的外观”。如果单元格 1 选择了单元格 7 更改等,则单元格 1 也是如此,如果我取消选择单元格 0 或单元格 6 都更改。

So i've tried cell?.selected == true, UICollectionView.allowsMultipleSelection = trueboth properties did not work for me.

所以我试过了cell?.selected == trueUICollectionView.allowsMultipleSelection = true这两个属性都不适合我。

am i doing something Wrong ?
is it an issue related to Cell Reuse ? if so could you explain this behavior ?

难道我做错了什么 ?
这是一个与细胞重用有关的问题吗?如果是这样,你能解释一下这种行为吗?

回答by Munahil

Do this :

做这个 :

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) 
{
         var cell = collectionView.cellForItemAtIndexPath(indexPath)
         if cell?.selected == true {
            cell?.backgroundColor = UIColor.orangeColor()
         }
          else 
             cell?.backgroundColor = UIColor.clearColor()
}

Also make a custom cell for UICollectionViewCell. Also change in your cellForItemAtIndexPathfunction :

还为 UICollectionViewCell 创建一个自定义单元格。也改变你的cellForItemAtIndexPath功能:

if cell?.selected == true 
      cell?.backgroundColor = UIColor.orangeColor()
    else 
       cell?.backgroundColor = UIColor.clearColor()

回答by glemoulant

First set collectionView.allowsMultipleSelection

第一套 collectionView.allowsMultipleSelection

then for exemple to change border on click :

然后例如在单击时更改边框:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let selectedCell : UICollectionViewCell = collectionView.cellForItem(at: indexPath)!

    selectedCell.layer.borderWidth = 2

}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    let unselectedCell : UICollectionViewCell = collectionView.cellForItem(at: indexPath)!

    unselectedCell.layer.borderWidth = 0
}