UITableViewCell多选的选定背景颜色

Bog*_*nov 42 selection selected uitableview ios swift

// Doesn't work
cell.selectionStyle = .Blue
//Works when the selection is not multiple, if it's multiple with each selection the previous one disappear...
let cellBGView = UIView()
cellBGView.backgroundColor = UIColor(red: 0, green: 0, blue: 200, alpha: 0.4)
cell.selectedBackgroundView = cellBGView
Run Code Online (Sandbox Code Playgroud)

任何答案如何设置所选单元格的背景颜色?

Ove*_*erD 92

所有上述答案都很好,但有点复杂,我喜欢.最简单的方法是在中添加一些代码cellForRowAtIndexPath.这样,您可以永远不必担心在取消选择单元格时更改颜色.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

    /* this is where the magic happens, create a UIView and set its
       backgroundColor to what ever color you like then set the cell's
       selectedBackgroundView to your created View */

    let backgroundView = UIView()
    backgroundView.backgroundColor = YOUR_COLOR_HERE
    cell.selectedBackgroundView = backgroundView
    return cell
}
Run Code Online (Sandbox Code Playgroud)

  • 我只想保持简单 (4认同)
  • 这是一个很好的解决方案.我唯一改变的是将backgroundView声明移到函数外部,并将backgroundView.backgroundColor集移动到viewDidLoad中.这样,您可以重复使用单个视图,并仅将颜色设置一次.不可否认,这可能是过早优化并传播您的代码,但这是我的风格;-)感谢您提供良好的解决方案! (2认同)

D K*_*ski 70

这对我有用:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var selectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
    selectedCell.contentView.backgroundColor = UIColor.redColor()
}

// if tableView is set in attribute inspector with selection to multiple Selection it should work.

// Just set it back in deselect 

override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    var cellToDeSelect:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
    cellToDeSelect.contentView.backgroundColor = colorForCellUnselected
}


//colorForCellUnselected is just a var in my class
Run Code Online (Sandbox Code Playgroud)


Ahm*_*tfy 33

斯威夫特3

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellIdentifier", for: indexPath)
    cell.selectionStyle = .none
    return cell
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特2

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
     let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellIdentifier", for: indexPath)
     cell.selectionStyle = .None
     return cell
}
Run Code Online (Sandbox Code Playgroud)


sup*_*org 13

Kersnowski方法的问题在于,当重新绘制单元格时,选择/取消选择时所做的更改将会消失.所以我会将更改移动到单元格本身,这意味着这里需要子类化.例如:

class ICComplaintCategoryCell: UITableViewCell {
    @IBOutlet var label_title: UILabel!
    @IBOutlet var label_checkmark: UILabel!

    override func layoutSubviews() {
        super.layoutSubviews()
        reload()
    }
    func reload() {
        if isSelected {
            contentView.backgroundColor = UIColor.red
        }
        else if isHighlighted{
            contentView.backgroundColor = UIColor.red
        }
        else {
            contentView.backgroundColor = UIColor.white
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在您的表视图中,委托只需调用reload:

if let cell = self.table.cellForRowAtIndexPath(path) as? ICComplaintCategoryCell {
    cell.reload()
}
Run Code Online (Sandbox Code Playgroud)

更新了Swift 3+,感谢@Bogy


Som*_*pta 12

斯威夫特4.2

对于多个选择,您需要将UITableView属性设置allowsMultipleSelectiontrue

myTableView.allowsMultipleSelection = true

如果您将UITableViewCell子类化,则可以setSelected(_ selected: Bool, animated: Bool)在自定义单元格类中重写方法。

 override func setSelected(_ selected: Bool, animated: Bool) {
     super.setSelected(selected, animated: animated)

     if selected {
         contentView.backgroundColor = UIColor.green
     } else {
         contentView.backgroundColor = UIColor.blue
     }
 }
Run Code Online (Sandbox Code Playgroud)


Des*_*vic 6

您还selectionStyle可以.none在界面构建器中将单元格设置为。与@AhmedLotfy 提供的解决方案相同,仅来自 IB。

在此处输入图片说明


Doc*_*oca 5

对于Swift 3,4和5,您可以通过两种方式执行此操作。

1)类:UITableViewCell

override func awakeFromNib() {
    super.awakeFromNib()
    //Costumize cell

    selectionStyle = .none
}
Run Code Online (Sandbox Code Playgroud)

要么

2)tableView cellForRowAt

    cell.selectionStyle = .none
Run Code Online (Sandbox Code Playgroud)

如果要设置特定单元格的选择颜色,请检查以下答案:https : //stackoverflow.com/a/56166325/7987502


SNu*_*del 5

UITableViewCell有一个属性multipleSelectionBackgroundViewhttps://developer.apple.com/documentation/uikit/uitableviewcell/1623226-selectedbackgroundview

只需创建一个您选择的UIView定义.backgroundColor并将其分配给您的单元格.multipleSelectionBackgroundView属性。