在分组的UITableView中设置UITableViewCell的backgroundColor,可以通过以下几种方法实现:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = UIColor.red
}
class CustomTableViewCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
self.backgroundColor = UIColor.red
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.backgroundColor = UIColor.red
return cell
}
在Storyboard中选择UITableViewCell,在Attributes Inspector面板中设置Background Color。
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.backgroundColor = UIColor.red
return cell
}
以上是在分组的UITableView中设置UITableViewCell的backgroundColor的几种方法,可以根据具体情况选择合适的方法。
领取专属 10元无门槛券
手把手带您无忧上云