UITableViewCell上的textLabel.backgroundColor不工作 [英] textLabel.backgroundColor on UITableViewCell does not work

查看:73
本文介绍了UITableViewCell上的textLabel.backgroundColor不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置我的UITableViewCells的标签backgroundColor,它绝对没有什么。

I'm trying to set the label backgroundColor of my UITableViewCells and it does absolutely nothing at all. I wonder if there's another way of doing this, so I'm asking!

我试过这个:

    cell.textLabel.backgroundColor = [UIColor redColor];
cell.backgroundColor = [UIColor redColor];

它不工作,还有什么?

谢谢!

推荐答案

我假设你在cellForRowAtIndexPath中尝试这个。

I assume you are trying this in cellForRowAtIndexPath.

根据 UITableViewCell类参考


注意:如果您要更改单元格的
背景颜色$ b单元格的背景颜色通过
声明的
backgroundColor属性UIView)你必须在
中执行它tableView:willDisplayCell:forRowAtIndexPath:
方法的委托,而不是in
tableView:cellForRowAtIndexPath:of
数据源。

Note: If you want to change the background color of a cell (by setting the background color of a cell via the backgroundColor property declared by UIView) you must do it in the tableView:willDisplayCell:forRowAtIndexPath: method of the delegate and not in tableView:cellForRowAtIndexPath: of the data source.

b
$ b

So do this:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = [UIColor redColor]; //must do here in willDisplayCell
    cell.textLabel.backgroundColor = [UIColor redColor]; //must do here in willDisplayCell
    cell.textLabel.textColor = [UIColor yellowColor]; //can do here OR in cellForRowAtIndexPath
}

如果您需要更好地控制单元格显示,一个简单的方法是在IB中设计它,并从cellForRowAtIndexPath中的nib加载它。请参阅此页面在表视图编程指南中。

If you need finer control over the cell display, an easy way is to design it in IB and load it from the nib in cellForRowAtIndexPath. See the "Loading Custom Table-View Cells From Nib Files" section on this page in the Table View Programming Guide.

这篇关于UITableViewCell上的textLabel.backgroundColor不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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