UITableViewCell的背景颜色 [英] Background color of a UITableViewCell

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

问题描述

我使用一个基本的表视图单元格,在IB中设置。我应用的唯一自定义是在cellForRowAtIndexPath,其中我只是设置单元格的标签的背景颜色(单元格本身的颜色)。

I'm using a basic table view cell, set up in IB. The only customization I apply is in cellForRowAtIndexPath, where I simply set the background color of the cell's label (not the color of the cell itself).

    [[cell textLabel] setBackgroundColor:[UIColor yellowColor]];

首次绘制表格时,不应用背景颜色。当我向下滚动时, 会应用于绘制的新单元格。同样,如果我向上滚动,顶部的单元格将重新绘制正确的背景颜色。

When the table is first drawn, the background color is not applied. As I scroll down, it is applied to new cells as they are drawn. Similarly, if I scroll back up, the top cells are re-drawn with the correct background color.

这背后的解释是什么?


编辑 - 这里是我的cellForRowAtIndexPath,

Edit - here's my cellForRowAtIndexPath, and the set up in IB:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ItemCell2"];
[[cell textLabel] setBackgroundColor:[UIColor yellowColor]];            
return cell;

}

推荐答案

你必须使用

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

从文件:


表视图在使用单元格绘制行之前将此消息发送到其委托,从而允许委托在显示之前自定义单元格对象。此方法为委托人提供了覆盖表视图较早设置的基于状态的属性的机会,例如选择和背景颜色。

A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color.

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

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