UICollectionView 的不可见单元格未取消选择 - Swift

Invisible cells of UICollectionView are not deselected - Swift

我的 swift 应用程序中有两个集合视图。一个是函数类别,另一个是函数。第一个作为第二个的过滤器。如果我 select "Cat1" 则仅显示带有标签 "Cat1" 的函数。这很好用。 功能类别 collectionview 是水平的,我需要滚动才能看到所有单元格。我的 issue/problem 已在其他主题中提及,但我找不到正确的答案或技术。

问题:如果我 select 一个类别,单元格的背景会改变,没问题。如果现在我完全滚动到 collectionview 的末尾和 select 最后一个单元格,这个更改为 selected 第一个(以前 selected)不是 deselected.. 我知道这是重复使用的单元格的问题,但不知道如何管理它。在我的代码下面:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// Working with functions categories collection view
      if collectionView == self.functionsCategoriesCollectionView {
          let cell = collectionView.cellForItem(at: indexPath) as! DevicePageFunctionsCategoriesCVCell
              cell.isHidden = false
              cell.cellView.isHidden = false
              cell.isSelected = true

              cell.cellView.clipsToBounds = true
              cell.cellView.layer.cornerRadius = 25
              cell.cellView.addGradiant(colors: [UIColor(red: 127.0/255.0, green: 127.0/255.0, blue: 127.0/255.0, alpha: 1.0).cgColor, UIColor(red: 47.0/255.0, green: 47.0/255.0, blue: 47.0/255.0, alpha: 1.0).cgColor], angle: 45)

          if cellSelectionIndexPath == indexPath {
              // it was already selected
              cellSelectionIndexPath = nil
              collectionView.deselectItem(at: indexPath, animated: true)
              cell.cellView.addGradiant(colors: [UIColor.clear.cgColor, UIColor.clear.cgColor], angle: 0)

              self.filtered = GlobalVariables.currentProduct.functions.filter { _ in
                  return true
              }

              self.functionsCollectionView.reloadData()

          } else {
              // wasn't yet selected, so let's remember it
              cellSelectionIndexPath = indexPath

              // Filter with seletec category name
              let cellCategoryName = ICDatabase.objects(FunctionCategory.self)[indexPath.row]

              self.filtered = GlobalVariables.currentProduct.functions.filter { function in
                  return function.functionCategory.contains(cellCategoryName)
              }

              self.functionsCollectionView.reloadData()
          }

      }
}

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

        if collectionView == self.functionsCategoriesCollectionView {

            if let cellToDeselect = collectionView.cellForItem(at: self.cellSelectionIndexPath) as? DevicePageFunctionsCategoriesCVCell {

                cellToDeselect.isSelected = false

                collectionView.deselectItem(at: self.cellSelectionIndexPath, animated: true)

                cellToDeselect.cellView.addGradiant(colors: [UIColor.clear.cgColor, UIColor.clear.cgColor], angle: 0)

                self.cellSelectionIndexPath = nil

                // Get all functions
                self.filtered = GlobalVariables.currentProduct.functions.filter { _ in
                    return true
                }

                self.functionsCollectionView.reloadData()
            }
        }
}

感谢您的帮助!

试试这个 -

var selectedIndexPath: IndexPath?

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath)

        cell.layer.backgroundColor = UIColor.black

        self.selectedIndexPath = indexPath
    }

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

        cell.layer.backgroundColor = UIColor.white

        self.selectedIndexPath = nil
    }

收集单元重用内存。所以你必须手动管理数据和UI。如果您的要求是任何时候select集合视图中只有一个类别单元格,请在集合视图中保留一个变量并在 didSelect 方法中更新它。

var selectedIndexPath: IndexPath?

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    selectedIndexPath = indexPath
    collectionView.reloadData()
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = UICollectionCell()
    ....
    cell.backgroundColor = UIColor.black
    if indexPath == selectedIndexPath {
        cell.backgroundColor = UIColor.red
    } 
}