【问题标题】:Changing cell background color in UICollectionView in Swift在 Swift 中更改 UICollectionView 中的单元格背景颜色
【发布时间】:2014-12-28 06:57:14
【问题描述】:

我正在使用水平集合视图来滚动日期。集合视图包含 30 个单元格。如果我选择第一个单元格,以指示选择,单元格背景颜色已从默认颜色红色更改为棕色。然后,如果我选择另一个单元格,则选定的单元格颜色已从红色变为棕色。但第一个单元格 BGColor 保持不变(棕色)。如何通过单击其他单元格更改为默认颜色?

       func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath 
   indexPath: NSIndexPath) -> UICollectionViewCell {

    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", 
   forIndexPath: indexPath) as myViewCell

    cell.date_label.text = arr_date[indexPath.item]

    }

        func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath 
       indexPath: NSIndexPath) {

        var cell = collectionView.cellForItemAtIndexPath(indexPath) as myViewCell

        if(cell.selected)
        {
            cell.backgroundColor = UIColor.brownColor()

        }
        else
        {
            cell.backgroundColor = UIColor.redColor()
        }

    }

【问题讨论】:

    标签: swift ios8 uicollectionview


    【解决方案1】:

    您可以将函数collectionView与参数didSelectItemAtIndexPath一起使用

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath 
       indexPath: NSIndexPath) {
    
           let selectedCell:UICollectionViewCell = myCollectionView.cellForItemAtIndexPath(indexPath)!
           selectedCell.contentView.backgroundColor = UIColor(red: 102/256, green: 255/256, blue: 255/256, alpha: 0.66)
    }
    

    这将为选定的UICollectionViewCell 创建一个常量,然后您只需更改背景颜色


    然后在取消选择时返回原来的颜色,必须使用函数collectionView和参数didDeselectItemAtIndexPath

    func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
            let cellToDeselect:UICollectionViewCell = myCollectionView.cellForItemAtIndexPath(indexPath)!
            cellToDeselect.contentView.backgroundColor = UIColor.clearColor()
    }
    

    然后你把颜色改成原来的!


    例如,这里是此代码在 filterApp 中的屏幕截图

    UICollectionView example

    【讨论】:

      【解决方案2】:
      var selectedIndex = Int ()
      
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
      {
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
      
          if selectedIndex == indexPath.row
          {
              cell.backgroundColor = UIColor.green
          }
          else
          {
              cell.backgroundColor = UIColor.red
          }
      
          return cell
      }
      
      func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
      {
          selectedIndex = indexPath.row
      
          self.yourCollctionView.reloadData()
      }
      

      可能很疯狂,但对我来说效果很好......!

      【讨论】:

        【解决方案3】:

        您可以保留最后选择的索引路径的副本,然后在您的didSelectItemAtIndexPath 中比较索引路径以查看它们是否不同。如果不同,请根据需要更改这些索引路径上两个单元格的颜色,然后将新的索引路径复制到旧的索引路径上。


        编辑

        再想一想,这应该通过单元格的backgroundViewselectedBackgroundView 属性来完成。将单元格出列后,您可以执行以下操作以让 iOS 处理更改。

        cell.backgroundView.backgroundColor = [UIColor redColor];
        cell.selectedBackgroundView.backgroundColor = [UIColor brownColor];
        

        【讨论】:

        • 谢谢安德鲁!但是如何维护新旧 Indexpath?
        • @McDonal_11 使用类成员变量
        • 当我使用cell.selectedBackgroundView.backgroundColor 时,我收到错误fatal error: unexpectedly found nil while unwrapping an Optional value。如果单元格已经存在,我是否必须先创建此视图?
        • 试试这个,需要包起来,cell.selectedBackgroundView?.backgroundColor = [UIColor brownColor];
        【解决方案4】:

        处理选定单元格背景颜色的最佳方法是观察属性isSelected。这可以处理单元格的选择和取消选择,否则在选择任何其他单元格时取消选择选定的单元格会很棘手。 下面是使用UICollectionViewCellisSelected属性的演示:

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

        【讨论】:

        • 这就是答案
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-18
        • 2011-02-17
        • 1970-01-01
        • 2021-03-25
        • 1970-01-01
        • 2020-02-02
        相关资源
        最近更新 更多