iOS Swift - 如何更改表视图的背景颜色?

37

我有一个简单的表格视图,我可以更改单元格的颜色,但尝试更改表格视图(背景部分)的颜色时无法工作...我已经尝试使用Storyboard...请问有人能帮忙吗?

6个回答

72

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
}

3
太好了!谢谢!它起作用了,不确定为什么在Storyboard中不起作用...我只是尝试了一下...感谢你的帮助。 - Vikas Seth
@Homam 我有一个静态的表格视图,但它对我来说不起作用。 - Nicholas
@Nicholas 在viewDidLoad()方法中添加以下语句:self.tableView.delegate = self - Homam
2
应该编写 'func tableView',而不是 'override func tableView'。 - Pokemon
我之前尝试了很多答案,但只有这个答案对我有效。先试一试,然后再感谢我吧! - Rabel Ahmed

17
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.contentView.backgroundColor = UIColor.yellowColor()
}

Swift 3

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

太棒了,这个简直太有效了... - undefined

7
如果您想通过Storyboard实现这一点,那么请在tableView中选择"table view cell"的"Content View",然后在属性检查器中转到View并更改其颜色,如下所示:

Xcode


5

使用此代码更改tableView的颜色

override func viewDidLoad() {
        super.viewDidLoad()

       // define tableview background color
        self.tableView.backgroundColor = UIColor.clearColor()
}

改变tableView单元格的颜色
cell.backgroundColor = UIColor.clearColor()

1

我来晚了!但是,以上所有答案都对我无效,所以我想分享一下我发现的有效方法,以防其他人也遇到同样的问题。

注意:我还有自己的自定义单元格类。不确定这是否会影响你的代码!

@IBDesignable class YourViewController: UITableViewController {

//IBInspectable and IBDesignable makes the properties accessible in storyboard
@IBInspectable var tableViewBackground: UIColor?
@IBInspectable var cellViewBackground: UIColor?

//in case you want to customize the text color, as well!
@IBInspectable var customTextColor: UIColor?


override func viewDidLoad() {
//your code
self.tableView.backgroundColor = tableViewBackground
}

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

    let cell = UITableViewCell()
    cell.textLabel?.textColor = customTextColor
    cell.backgroundColor = cellViewBackground
    return cell
}
//the rest of your code
}

查找您的可检查项:

1). 在tableView的viewController的Storyboard菜单中,点击yourViewController。这是viewController内部的第一个下拉菜单。(第一个下拉菜单将包含您的tableView和cellView。根据您的特定项目,它还可以包含其他好东西)。

2). 在属性检查器中,您将看到一个标题,其中包含您的viewController的名称和我们上面定义的@IBInspectable属性!!

3). 然后您可以在那里随意更改它们。

希望这有所帮助!


3
欢迎来到 Stack Overflow 的派对。 :) - Ben

1

你有:

  1. 单元格背景
  2. 单元格内容视图

将ContentView放置在SB中的默认位置(透明),并在你的单元格方法中的awakeFromNib中,只需:

self.backgroundColor = UIColor.SomeColor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接