为什么我的收藏夹视图单元格颜色选择不起作用

时间:2019-02-13 23:35:56

标签: swift colors background uicollectionview cell

我的单元在StoryBoard上设置为蓝色,但是即使当用户选择单元时更改单元backgroundColor时,它们仍为蓝色(不是红色)。我的代码有什么问题?

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "throneCell", for: indexPath) as! CharacterViewCell
    let character = characters[indexPath.row]
    cell.backgroundColor = UIColor.red
    onBoardingService.pickCharacter(character: character)
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "throneCell", for: indexPath) as! CharacterViewCell
    let character = characters[indexPath.row]
    onBoardingService.removeCharacter(character: character)
}

cell on Storyboard

2 个答案:

答案 0 :(得分:1)

替换

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "throneCell", for: indexPath) as! CharacterViewCell

使用

let cell = collectionView.cellForItem(at: indexPath) as! CharacterViewCell
  

请勿使用dequeueReusableCell中的cellForItemAt,因为它会返回不是被点击的单元格

didDeselectItemAt可能在单元格不在此处时被调用,

guard let cell = collectionView.cellForItem(at: indexPath) as? CharacterViewCell else { return }

 var selectedIndex = 0

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "throneCell", for: indexPath) as! CharacterViewCell
    let character = characters[indexPath.row]

    if selectedIndex == indexPath.row {
         onBoardingService.pickCharacter(character: character)
         cell.backgroundColor =  UIColor.red 
    else {
          cell.backgroundColor = UIColor.clear
          onBoardingService.removeCharacter(character: character)
    }



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

    selectedIndex = indexPath.row
    collectionView.reloadData()
}

删除此方法

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "throneCell", for: indexPath) as! CharacterViewCell
    let character = characters[indexPath.row]
    onBoardingService.removeCharacter(character: character)
}

关于单个项目的选择

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {  
    selectedIndex = selectedIndex == indexPath.row ? nil : indexPath.row 
    collectionView.reloadData()
}

并声明

var selectedIndex:Int?

答案 1 :(得分:0)

尝试在didDeselectItemAt上添加cell.backgroundColor = UIColor.clear。

赞:

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
  let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "throneCell", for: indexPath) as! CharacterViewCell
  let character = characters[indexPath.row]
  cell.backgroundColor = UIColor.clear
  onBoardingService.removeCharacter(character: character)
}