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

Gre*_*reg 56 cocoa-touch uitableview uikit ios

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

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

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

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

chr*_*ris 70

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

cell.contentView.backgroundColor = [UIColor redColor];
Run Code Online (Sandbox Code Playgroud)

  • 如果要最小化代码中必须进行的样式量,还可以使用Interface Builder中的"用户定义的运行时属性"部分进行设置 - 只需使用"contentView.backgroundColor"作为键路径,选择"颜色"作为键入,然后使用Interface Builder颜色选择器为值选择颜色.只要确保你是否会采用这种方法将其作为团队的标准建立起来,否则你最终会为未来的开发人员创造一个长长的谜团,他们无法弄清楚到底在哪里背景颜色正在设定. (2认同)

Shi*_*ack 16

对我有用的是创建我自己的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];
Run Code Online (Sandbox Code Playgroud)


occ*_*lus 11

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

推荐阅读:

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

然后:

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


Sub*_*bbu 7

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

这个作品!!


Gre*_*reg 0

(尝试这个)

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