gpt4 book ai didi

ios - 如何更改表格 View 单元格的选定颜色?

转载 作者:搜寻专家 更新时间:2023-10-31 21:52:15 24 4
gpt4 key购买 nike

当我在表格 View 中选择一个单元格时,它会变成这种白色。我想改变它。

enter image description here

我尝试使用 if 语句,但它不起作用。

这是我使用的代码。

override func tableView(_ tableView: UITableView, cellForRowAt 
indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath)

if cell.isSelected == true {
cell.backgroundColor = .blue
} else {
cell.backgroundColor = .red
}

return cell

}

最佳答案

您可以执行其中任何一项 -

  1. 更改单元格的 selectionStyle 属性。

例如:如果将其更改为 UITableViewCellSelectionStyleGray,它将是灰色的。

 cell.selectionStyle = UITableViewCellSelectionStyleGray;
  1. 更改 selectedBackgroundView 属性。

    let bgColorView = UIView()
    bgColorView.backgroundColor = UIColor.red
    cell.selectedBackgroundView = bgColorView

swift 4 修正:

    cell.selectionStyle = UITableViewCellSelectionStyle.gray

关于ios - 如何更改表格 View 单元格的选定颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44752644/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com