簡體   English   中英

在 Swift 中的 UICollectionView 中更改單元格背景顏色

[英]Changing cell background color in UICollectionView in Swift

我正在使用水平集合視圖來滾動日期。 集合視圖包含 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()
        }

    }

您可以使用帶有參數didSelectItemAtIndexPath的函數collectionView

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創建一個常量,然后您只需更改背景的顏色


然后為了在取消選擇時返回到原始顏色,您必須使用帶有參數didDeselectItemAtIndexPath的函數collectionView

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

然后你把顏色改成原來的顏色!


例如,這里是 filterApp 中此代碼的屏幕截圖

UICollectionView 示例

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

可能很瘋狂,但它對我來說很好用......!

您可以維護最后選擇的索引路徑的副本,然后在您的didSelectItemAtIndexPath比較索引路徑以查看它們是否不同。 如果不同,請根據需要更改這些索引路徑中兩個單元格的顏色,然后將新索引路徑復制到舊索引路徑上。


編輯

再考慮一下,這應該通過單元格的backgroundViewselectedBackgroundView屬性來完成。 單元出列后,您可以執行以下操作讓 iOS 處理更改。

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

處理所選單元格背景顏色的最佳方法是觀察屬性isSelected 這處理單元格的選擇和取消選擇,否則在選擇任何其他單元格時取消選擇選定的單元格會很棘手。 下面是使用UICollectionViewCell isSelected屬性的UICollectionViewCell

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM