iOS Swift - 如何更改表格 View 的背景颜色?

标签 ios uitableview swift

我有一个简单的表格 View ,我可以更改单元格的颜色,但是当尝试更改表格 View (背景部分)的颜色时它不起作用...我通过 Storyboard 尝试过...有人可以吗帮助

最佳答案

首先在viewDidLoad中设置tableView的背景色如下:

override func viewDidLoad() {
    super.viewDidLoad()   
    self.tableView.backgroundColor = UIColor.lightGrayColor()
}

现在添加这个方法:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.backgroundColor = UIColor.clearColor()
}

Swift 3 中,使用下面的方法代替上面的方法:

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.backgroundColor = UIColor.lightGray
}

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.backgroundColor = UIColor.clear
}

关于iOS Swift - 如何更改表格 View 的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30679701/

相关文章:

ios - 快速更改 UISlider 的高度

iphone - 根据字符串的大小设置行高?

ios - 正确使用 beginBackgroundTaskWithExpirationHandler

ios - 按下按钮时应用程序崩溃

ios - 将来自 REST API 的 JSON 数据快速填充到 TableView 不起作用

ios - Callkit 来电界面

objective-c - UITableView 从 subview 中抓取触摸事件

ios - UITableView 中的单元格只有在 iOS 中选择后才会更新(快速)

ios - 无法使用标识符单元 XX 错误使单元出队 - 无法弄清楚如何解决

Swift 2.0 GeoCoder 参数列表