ios UI集合视图单元格背景选定时颜色不变

jfewjypa  于 2023-01-06  发布在  iOS
关注(0)|答案(7)|浏览(133)

我试图改变单元格的背景色时,它被选中。但单元格的背景色没有改变。

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

nzrxty8p1#

你不需要在选择时手动改变背景色,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
}
vx6bjr1n

vx6bjr1n2#

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

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    let selectedCell = collectionView.cellForItemAtIndexPath(indexPath)

    selectedCell?.backgroundColor = UIColor.blueColor()
}
g6baxovj

g6baxovj3#

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

    if let cell = collectionView.cellForItem(at: indexPath) {
        cell.backgroundColor = cell.isSelected ? .black : .white
    }
}
nukf8bse

nukf8bse4#

应使用以下两个集合视图委托方法:

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
}
vql8enpb

vql8enpb5#

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
    let cell = collectionView.cellForItem(at: indexPath) as! CustomCell
    cell.cellView.backgroundColor = .red
    
}
brccelvz

brccelvz6#

我认为处理一个单元格的背景颜色应该是一个单元格的一部分,通过观察它的属性isSelected。它处理选择和取消选择一个单元格,否则它将是棘手的取消选择一个选定的单元格在选择任何其他单元格,例如:

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

alen0pnh7#

你可以在自定义单元格级别上指定选定的和正常的背景视图。这是苹果实际上推荐的,这里是他们自己的样本和苹果文档的链接:

override func awakeFromNib() {
super.awakeFromNib()
    
    let redView = UIView(frame: bounds)
    redView.backgroundColor = #colorLiteral(red: 1, green: 0, blue: 0, alpha: 1)
    self.backgroundView = redView

    let blueView = UIView(frame: bounds)
    blueView.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 1, alpha: 1)
    self.selectedBackgroundView = blueView
}

https://developer.apple.com/documentation/uikit/uicollectionviewdelegate/changing_the_appearance_of_selected_and_highlighted_cells

相关问题