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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30679701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:14:35  来源:igfitidea点击:

iOS Swift - How to change background color of Table View?

iosuitableviewswift

提问by Vikas Seth

I have a simple table view, I can change the colors of cells, but when trying to change the color of Table View (Background part) it is not working... I tried it via Storyboard... Can someone please help

我有一个简单的表格视图,我可以更改单元格的颜色,但是当尝试更改表格视图(背景部分)的颜色时,它不起作用......我通过故事板尝试过......有人可以帮忙

回答by Homam

First set the background color of the tableView in viewDidLoadlike below:

首先设置 tableView 的背景颜色,viewDidLoad如下所示:

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

Now add this method:

现在添加这个方法:

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

In Swift 3, use below methods instead of above one:

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
}

回答by Lucas Farah

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

Swift 3

斯威夫特 3

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

回答by user6082493

If you want to achieve that via storyboard then select "Content View" of "table view cell" in your tableView then in attribute Inspector go to View and change its color like so:

如果您想通过故事板实现这一点,请在您的 tableView 中选择“表格视图单元格”的“内容视图”,然后在属性检查器中转到“视图”并更改其颜色,如下所示:

Xcode

Xcode

回答by IOS Singh

use this code for change tableView color

使用此代码更改 tableView 颜色

override func viewDidLoad() {
        super.viewDidLoad()

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

for change tableView cell color

用于更改 tableView 单元格颜色

cell.backgroundColor = UIColor.clearColor()

回答by Brigitte Alese

I'm a little late to the game! But, none of the above answers worked for me, so just going to share, what I found worked, in case anyone else is jumping on this thread.

我玩游戏有点晚了!但是,上述答案都不适用于我,所以我只想分享我发现的工作,以防其他人跳上这个线程。

NOTE: I also have my own custom cell class. Not sure if that will affect your code!

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

@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
}

Finding your inspectables:

查找您的可检查对象:

1). In the Storyboard menu for the viewController of your tableView, click on yourViewController. The first dropdown inside the viewController. (The first dropdown will contain your tableView and cellView. It can also contain other goodies depending on your particular project).

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

2). In the attributes inspector, you'll see a heading with the name of your viewController and the @IBInspectable properties we defined above!!

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

3). You can then change them however you want there.

3)。然后,您可以随意更改它们。

Hope this helps!

希望这可以帮助!

回答by William

You have:

你有:

  1. Cell background AND
  2. Cell ContentView
  1. 细胞背景 AND
  2. 单元格内容视图

Put ContentView like Default (transparent) in SB, and in your Cell's Method in awakeFromNib, just:

将 ContentView 像 Def​​ault (transparent) 一样放在 SB 中,并放在你的 Cell's Method 中的awakeFromNib 中,只需:

self.backgroundColor = UIColor.SomeColor