在普通样式的UITableView中更改自定义UITableViewCell单元格的背景颜色,可以通过以下步骤实现:
awakeFromNib
方法,在该方法中设置单元格的背景颜色。override func awakeFromNib() {
super.awakeFromNib()
// 设置单元格的背景颜色
contentView.backgroundColor = UIColor.red
}
cellForRowAt
中设置单元格的背景颜色。func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
// 根据数据源动态设置单元格的背景颜色
cell.contentView.backgroundColor = UIColor.red
return cell
}
didSelectRowAt
中设置单元格的背景颜色。func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
// 设置选中单元格的背景颜色
cell.contentView.backgroundColor = UIColor.blue
}
didDeselectRowAt
中设置单元格的背景颜色。func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
// 设置取消选中单元格的背景颜色
cell.contentView.backgroundColor = UIColor.red
}
通过以上方法,可以在普通样式的UITableView中更改自定义UITableViewCell单元格的背景颜色。
领取专属 10元无门槛券
手把手带您无忧上云