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]; … }
以上代码在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]]; }
参考文章: