ios 为什么 UITableViewCell 背景颜色不起作用(在界面构建器中设置)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5166747/
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-30 19:05:35  来源:igfitidea点击:

Why doesn't UITableViewCell background color work (set in interface builder)?

iosuitableviewcocoa-touchuikit

提问by Greg

Why doesn't UITableViewCell background color work (set in interface builder)?

为什么 UITableViewCell 背景颜色不起作用(在界面构建器中设置)?

I note from some searching that the follow code set in your custom subclass of UITableViewController does work (see below):

我从一些搜索中注意到 UITableViewController 的自定义子类中设置的以下代码确实有效(见下文):

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.backgroundColor = [UIColor redColor];
}

But I would still like to just understand why interface builder has a background color setting for the TableViewCell, which effectively doesn't seem to work?

但我仍然想了解为什么界面构建器为 TableViewCell 设置了背景颜色,但实际上似乎不起作用?

采纳答案by Greg

(try this)

(尝试这个)

Apple's IB's design is generic to be applied to multiple kinds of components, and is also targeted at a highly technical audience (developers). Hence it has not been designed (as an application) to fully customise the interface properly for each component/situation type. So in this case it is the case the ability to seemingly set the background-color is somewhat misleading.

Apple 的 IB 设计是通用的,适用于多种组件,也针对技术含量高的受众(开发人员)。因此,它还没有被设计(作为应用程序)来为每个组件/情况类型完全自定义界面。因此,在这种情况下,看似设置背景颜色的能力有些误导。

回答by chris

It's a bit late, but I just ran into this issue... Setting the background color on the contentViewworks fine:

有点晚了,但我刚刚遇到了这个问题......在contentView作品上设置背景颜色很好:

cell.contentView.backgroundColor = [UIColor redColor];

回答by Shiprack

What worked for me is creating my own UIView object to be set as the background view of the UITableViewCell. In the cellForRowAtIndexPath:

对我有用的是创建我自己的 UIView 对象以设置为 UITableViewCell 的背景视图。在 cellForRowAtIndexPath 中:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
bgview.opaque = YES;
bgview.backgroundColor = [UIColor orangeColor];
[cell setBackgroundView:bgview];

回答by occulus

Trying to set the background colour of the UITableViewCell itself is not how you should be doing it. A table cell has a collection of five useful views you can access, one of which is backgroundView.

尝试设置 UITableViewCell 本身的背景颜色不是你应该做的。表格单元格包含五个可以访问的有用视图的集合,其中之一是backgroundView.

Recommended reading:

推荐阅读:

http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html

http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html

and then:

进而:

http://cocoawithlove.com/2010/12/uitableview-construction-drawing-and.html

http://cocoawithlove.com/2010/12/uitableview-construction-drawing-and.html

回答by Subbu

You should always provide changes for cells in tableView:willDisplayCell:forRowAtIndexPath:instead of the tableView:cellForRowAtIndexPath:method, as per Apple Documentation.

根据 Apple 文档,您应该始终为单元格提供更改tableView:willDisplayCell:forRowAtIndexPath:而不是tableView:cellForRowAtIndexPath:方法。

This Works !!

这有效!!

回答by Warewolf

[cell.textLabel setBackgroundColor:[UIColor clearColor]];
[cell.contentView setBackgroundColor:[UIColor redColor]];

回答by felipecamposclarke

Make sure before defining a cell color your backgroundViewis null

在定义单元格颜色之前确保您的backgroundView值为空

cell.backgroundView = nil;
cell.backgroundColor = [UIColor redColor];

回答by Bogatyr

This is simple and works: in interface builder, add a view object to the UITableViewCell of the same dimensions, and place your controls inside that view. Then you get whatever background color you want.

这很简单且有效:在界面构建器中,将视图对象添加到相同维度的 UITableViewCell,并将您的控件放在该视图中。然后你得到你想要的任何背景颜色。

Edit: here's a reason to accept, it just struck me: take a careful look at what you get when you double click on a UITableViewCell in IB that you place into your nib, to start adding stuff to it: you don't get a plain rectangle looking view, you get an oval rectangle that says quite clearly: Content View. So all you can do is set the content view of the UITableViewCell in IB, and setting the background color property of the content view does not produce the color change in the table. As the docs say, UITableViewCells are complex beasts with many parts with a lot going on behind the scenes in terms of the UITableView adjusting it and fiddling with it.

编辑:这是一个接受的理由,它让我印象深刻:仔细看看当你在 IB 中双击一个 UITableViewCell 并放入你的笔尖时你会得到什么,开始向它添加东西:你没有得到普通矩形查看视图,您会得到一个椭圆形矩形,上面写得很清楚:内容视图。所以你能做的就是在IB中设置UITableViewCell的content view,设置content view的background color属性不会产生表格中的颜色变化。正如文档所说,UITableViewCells 是复杂的野兽,有很多部分,在 UITableView 调整它和摆弄它方面有很多幕后工作。

回答by Splendid

Y not, use setBackgroundColor

不是,使用 setBackgroundColor

self.table.separatorColor=[UIColor whiteColor];
[table setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];

回答by Csabi

It works perfectly, if you pick the tableview and set background to for example red the whole table will be red. maybe you do not linked the table or something

它工作得很好,如果您选择 tableview 并将背景设置为例如红色,则整个表格将是红色的。也许你没有链接表格或其他东西

hope it helped

希望它有帮助