【问题标题】:Why is My Collection View Cell Color Selection Not Working为什么我的收藏视图单元格颜色选择不起作用
【发布时间】:2019-07-07 22:18:56
【问题描述】:

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

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

【问题讨论】:

    标签: swift colors background uicollectionview cell


    【解决方案1】:

    替换

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

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

    不要在cellForItemAt 中使用dequeueReusableCell,因为它会返回一个不是被点击的单元格

    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:致命错误:在展开可选值时意外发现 nil”后会这样说
    • 非常感谢!我如何让用户只选择 3 个单元格并根据排名(第 1、第 2、第 3 选择)跟踪选择的单元格?
    【解决方案2】:

    尝试在 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)
    }
    

    【讨论】:

    • 还是不行!你认为我还应该尝试什么?
    • 尝试在 didSelectRow 上添加 tableview.reloadData()
    • 我尝试了collectionView.reloadData,但仍然无法工作,请帮助! D:
    猜你喜欢
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 1970-01-01
    相关资源
    最近更新 更多