UITableView的cell设置背景图

 4.0版本中, 直接在tableview的datasource委托中设置textLabel和detailTextLabel的背景色为透明即可, 如下

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
…
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
…
}

UITableViewCell

以上代码在3.0中却有问题, 运行后结果如下所示

UITableViewCell in 3.0

这个应该是3.0的一个bug, 后来找到解决方案如下:

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

在tableview的委托方法tableView:willDisplayCell:forRowAtIndexPath:中, 设置textLabel和detailTextLabel的背景色透明.

如果表格单元采用UITableViewCell来操作, 还可以采用下面链接中的方法:

- (void)layoutSubviews {
[super layoutSubviews];
[self setBackgroundColor:[UIColor clearColor]];
}

参考文章:

http://www.sixtemia.com/journal/2009/07/23/how-to-change-the-uitableviewcell-backgroundcolor-backgroundview/

http://stackoverflow.com/questions/281515/how-to-customize-the-background-color-of-a-uitableviewcell/1220985#1220985

XeonWell Studio