ios 快速设置UiCollectionView中单元格的选中颜色

bwntbbo3  于 2023-02-10  发布在  iOS
关注(0)|答案(7)|浏览(245)

我正在使用CollectionView显示数据。
每个单元格的选中和未选中颜色都在那里
但我想设置默认颜色的第一个单元格在collectionview.

如果有10个数据,我第一个单元格应该像屏幕截图
如何实现这?当我设置颜色在indexpath这个问题发生

wwtsj6pe

wwtsj6pe1#

对于基于单元格选定状态的两种不同颜色,您可能需要将UICollectionViewCell子类化如下:

import UIKit 

class YourCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var imageView: UIImageView! // for example - some UI element inside cell ...

    override var isSelected: Bool {
        didSet {
            self.contentView.backgroundColor = isSelected ? UIColor.blue : UIColor.yellow
            self.imageView.alpha = isSelected ? 0.75 : 1.0
        }  
      }
    }

// and below you will have your original code

class YourViewController: UICollectionViewController {

... etc

回答您的问题-对于第一个单元格的特殊样式

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

        let cell = collectionView.dequeueReusableCell ... 

        if(indexPath.row == 0) { //for first cell in the collection
            cell.backgroundColor = UIColor.orange
        } else {
            cell.backgroundColor = UIColor.green
        }
yvfmudvl

yvfmudvl2#

这是我的代码为这个问题。希望它保存时间为您:)

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as! CustomCell
    
    if let indexPaths = collectionView.indexPathsForSelectedItems, let indexP = indexPaths.first {
        cell.bgView.backgroundColor = indexP == indexPath ? .white : .lightGray
    }
    
    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if let cell = collectionView.cellForItem(at: indexPath) as? CustomCell {
        cell.bgView.backgroundColor = .white 
    }
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    guard let cell = collectionView.cellForItem(at: indexPath) as? CustomCell else {
        return
    }
    
    cell.bgView.backgroundColor = .lightGray
}
slmsl1lt

slmsl1lt3#

调用选择项在索引路径:动画:滚动位置:UICollectionView的reloadData方法之后的方法
例如:

let collectionView = UICollectionView()
collectionView.reloadData()     
collectionView.selectItemAtIndexPath(NSIndexPath(forItem: 0, inSection: 0), animated: true, scrollPosition: .None)
34gzjxbg

34gzjxbg4#

根据@pedroun的回答,这里是我更新的答案,让你的第一个单元格的默认颜色。希望这对你有帮助。

@IBOutlet weak var cellImageview: UIImageView!

var cellIndex: Int? {
    didSet {
        guard let index = cellIndex else { return }
        if index != 0 {
            contentView.backgroundColor = UIColor.blueColor()//change this to your unselected cell color.
        }else{
            contentView.backgroundColor = UIColor.redColor()//this is default color for your first cell.
            cellImageview.alpha = 1.0
        }
    }
}

override var selected: Bool {
    didSet {
        contentView.backgroundColor = selected ? UIColor.redColor(): UIColor.blueColor()//change colors as per your requirements
        cellImageview.alpha = selected ? 0.75 : 1.0
    }
}

对于cellIndex属性,需要在cellForRowAtIndexPath中将其设置为cell.cellIndex = indexPath.row

0x6upsns

0x6upsns5#

UICollectionViewDelegate中有两个委托方法。
did取消选择索引路径上的项目并did选择索引路径上的项目
点击集合单元格时,可以得到该单元格的indexPath,也可以通过cellForItemAtIndexPath得到集合单元格
我在UITableView中有过经验,我认为UICollectionView与以下逻辑相同。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.row < arrData.count && arrData.count > 0{
        if let selectedCell = tableView.cellForRowAtIndexPath(indexPath) as? GARatesCell {
        selectedCell.viewBG.backgroundColor = COLOR_NAV_BAR
        selectedCell.lblTitle.textColor = UIColor.whiteColor()
        btnShare.backgroundColor = COLOR_NAV_BAR
        self.selectedIndex = indexPath.row
        }
    }
}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

    if  indexPath.row < arrData.count && arrData.count > 0{
        let deselectedCell = tableView.cellForRowAtIndexPath(indexPath) as? GARatesCell
        if deselectedCell != nil {
            deselectedCell?.viewBG.backgroundColor =  COLOR_BG_BAR
            deselectedCell?.lblTitle.textColor = COLOR_NAV_BAR
        }
    }

}
csbfibhn

csbfibhn6#

我知道这不是个完美的解决方案。但它确实有效。

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    collectionView.visibleCells.forEach {
        $0.backgroundColor = .white
    }
}
ohtdti5x

ohtdti5x7#

我的建议是在单元格加载时在view controller中添加以下行(即在didLoadwillAppear中添加)。

self.DateCollectionView.selectItem(at: IndexPath(item: 0, section: 0), animated: false, scrollPosition: .left)

然后在UICollectionViewCell类中添加以下方法:

class DateCollectionCell: UICollectionViewCell {
    
    @IBOutlet private weak var dateLable: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    override var isSelected: Bool {
        didSet{
            if self.isSelected {
                self.backgroundColor = UIColor(hexString: "#6FD5C9")
            } else {
                self.backgroundColor = UIColor(hexString: "#FFFFFF")
            }
        }
    }
}

相关问题