代码之家  ›  专栏  ›  技术社区  ›  Murat Kaya

UICollectionviewCell背景色在选中时不更改

  •  3
  • Murat Kaya  · 技术社区  · 8 年前

    我正在尝试更改选定单元格的背景颜色。但是细胞背景颜色没有改变。

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CategoryCollectionViewCell
        let category = self.categories[indexPath.row]
        switch cell.isSelected {
        case true:
            cell.backgroundColor = .black
        default:
            cell.backgroundColor = .white
        }
        cell.setNeedsDisplay()    
    }
    
    6 回复  |  直到 4 年前
        1
  •  4
  •   jamesk    8 年前

    您不需要在选择时手动更改背景颜色。UICollectionViewCell有一个名为 selectedBackgroundView 正是为了这个目的。

    collectionView(_:cellForItemAt:) 委托方法如下:

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CategoryCollectionViewCell
    
        cell.selectedBackgroundView = UIView(frame: cell.bounds)
        cell.selectedBackgroundView!.backgroundColor = .black
    
        return cell
    }
    
        2
  •  3
  •   keith    8 年前

    在您的didSelect委托方法中尝试以下操作:

    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        let selectedCell = collectionView.cellForItemAtIndexPath(indexPath)
    
        selectedCell?.backgroundColor = UIColor.blueColor()
    }
    
        3
  •  1
  •   Callam    8 年前
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
        if let cell = collectionView.cellForItem(at: indexPath) {
            cell.backgroundColor = cell.isSelected ? .black : .white
        }
    }
    
        4
  •  0
  •   user4886069 user4886069    8 年前

    您应该使用以下两种集合视图委托方法:

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CategoryCollectionViewCell
    
      cell.backgroundColor = .black
    }
    

    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CategoryCollectionViewCell
    
      cell.backgroundColor = .white
    }
    
        5
  •  0
  •   Jaykant    4 年前
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let cell = collectionView.cellForItem(at: indexPath) as! CustomCell
        cell.cellView.backgroundColor = .red
        
    }
    
        6
  •  0
  •   Aamir    4 年前

    我认为通过观察细胞的特性,处理细胞的背景颜色应该是细胞的一部分 isSelected 它处理单元格的选择和取消选择,否则在选择任何其他单元格时取消选择所选单元格会很棘手,例如:

    class MyCustomCollectionViewCell: UICollectionViewCell {
        override var isSelected: Bool {
            didSet {
                contentView.backgroundColor = isSelected ? .red : .white
            }
        }
    }