如何在 iOS 上修改 TableView 项目的背景颜色?


修改表视图项目的背景颜色不同于修改表视图的背景颜色。新手程序员经常将这两件事混为一谈,在本帖中,我们将学习如何修改 TableView 项目的背景颜色,即单元格。

因此,让我们开始吧。

如需修改表视图单元格的背景颜色,你应当修改单元格的 contentView.backgroundColor 属性。

Add the below code in your cellForRowAt indexPath method,
cell.contentView.backgroundColor = UIColor.cyan

你的方法应当类似如下内容,

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
   cell.contentView.backgroundColor = UIColor.cyan
   return cell
}

现在运行项目查看效果。

更新于: 30-Aug-2019

2K+ 次浏览

开启你的 职业生涯

完成课程并获得认证

开始
广告
© . All rights reserved.