无法在IB中设置UITableViewCell的背景颜色 [英] Can't set background color of UITableViewCell in IB

查看:119
本文介绍了无法在IB中设置UITableViewCell的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一堆单元格,我在表视图中重复使用。所有这些单元格在它们内部只有不同的 UILabel ,一些 UIImageView (没有覆盖整个单元格)。

I have created a bunch of cells which I reuse in a table view. All of those cells just have different UILabels inside them and some UIImageViews (nothing covers the complete cell).

在IB中设置背景颜色没有效果(总是白色或透明,不能说它们中的哪一个)。但是如果我按下Command-R(模拟界面)单元格在模拟器中有正确的背景颜色。

Setting the background color in IB has no effect (always white or transparent, can't say which one of them). But if I press Command-R (simulate interface) the cell has the correct background color in the simulator.

我试图设置它在 tableView :cellForRowAtIndexPath:但它不工作,就像我想的。

I tried to set it in tableView:cellForRowAtIndexPath: but it doesn't work like I would think either.

这样做的诀窍:

cell.contentView.backgroundColor = [UIColor redColor];

但这些都没有效果(即使我设置了 cell.contentView。 backgroundColor clearColor ):

but these have no effect (even if I set the cell.contentView.backgroundColor to clearColor):

cell.backgroundView.backgroundColor = [UIColor redColor];

cell.backgroundColor = [UIColor redColor];

我设置了IB中的所有布局/字体/背景。有什么想法为什么这在这种情况下不工作?

I set all the layout/font/background stuff in IB. Any idea why this isn't working in this case?

为什么我需要修改 contentView backgroundColor 而不是 backgroundView 的?

Why do I need to modify the contentView's backgroundColor and not the backgroundView's?

似乎是一个常见的问题。可能有人请指出正确的方向(最后)了解背景颜色在表格视图单元格中的处理方式。

It seems to be a common issue. Could somebody please point me in the right direction to (finally) understand how background colors are handled within a table view cell.

推荐答案

要更改单元格的背景颜色,您必须在willDisplayCell:forRowAtIndexPath:方法中进行。这在靠近顶部的 UITableViewCell文档中有所提及一个盒装的笔记。所以你需要:

To change the background color of the cell, you have to do it in the willDisplayCell:forRowAtIndexPath: method. This is mentioned in the UITableViewCell documentation near the top in a boxed note. So you need:

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

contentView只是推荐的子视图来放置自定义控件,当表编辑正在进行时,正确布局。

The contentView is just the recommended subview to put custom controls in so that the cell gets layed out properly when table editing is going on.

这篇关于无法在IB中设置UITableViewCell的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆