未选中UICollectionView的不可见单元格-Swift

问题描述 投票:0回答:3

我在快速应用程序中有两个收藏夹视图。一个是功能类别,另一个是功能类别。第一个充当第二个的过滤器。如果选择“ Cat1”,则仅显示带有标签“ Cat1”的功能。这很好。函数类别collectionview是水平的,我需要滚动查看所有单元格。我的问题/问题已在其他主题中提到,但我找不到正确的答案或技巧。

问题:如果选择一个类别,则单元格的背景会发生变化,很好。如果现在我完全滚动到collectionview的末尾并选择最后一个单元格,则不会取消选择与第一个(先前选择的)单元格相同的更改。那。在我的代码下面:

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()
            }
        }
}

感谢您的帮助!

swift uicollectionview didselectrowatindexpath
3个回答
0
投票

尝试一下-

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
    }

0
投票

收集单元重用内存。因此,您必须手动管理数据和UI。如果您需要随时在集合视图中仅选择一个类别单元格,则在集合视图中保留一个变量,并在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
    } 
}

0
投票
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if collectionView == self.functionsCategoriesCollectionView{
        let selectedItems = collectionView.indexPathsForVisibleItems
        for indexPath in selectedItems{
            collectionView.deselectItem(at: indexPath, animated:true)
            if let cell = collectionView.cellForItem(at: indexPath) as? YourCollectionViewCell {
                //Do whatever you want with the deselect cell
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.