UITableViewCell通过cell.backgroundColor设置背景颜色为什么没有效果

57 篇文章 0 订阅

在做ios6的适配的时候发现,无论我是把cell的backgroundColor设置颜色还是增加一个view设置背景色,在cell上都没有效果。

查了一下,找到如下文章,解决了这个问题。


链接:http://blog.sina.com.cn/s/blog_6531b9b80101c30b.html


今天同事一个UITableViewCell得背景设置得时候产生了疑惑

用cell.backgroundColor = [UIColor blueColor];

设置背景没有效果。

就调研了下,很快在伟大的stackoverflow找到了答案:

UITableViewCell通过cell.backgroundColor设置背景颜色为什么没有效果
查字典的结果:

有一个2009年或者2010的WWDC视频提到了这个问题。tableview会调整背景来管理cells的选择状态,这就是为什么你能且只能在willDisplayCell这个方法里面修改它的实现的原因。

下面是验证过程:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{

   static NSString* textCellIndentify = @"textCellIndentify";

    UITableViewCell* cell  = [tableView dequeueReusableCellWithIdentifier: textCellIndentify];

    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: textCellIndentify];

          cell.backgroundColor = [UIColor redColor];

    }

//断点, 可以看到我们的cell的背景颜色是已经修改了的。

UITableViewCell通过cell.backgroundColor设置背景颜色为什么没有效果
    return cell;

}

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

{

//断点,可以看到cell的颜色又被系统修改回来了

UITableViewCell通过cell.backgroundColor设置背景颜色为什么没有效果
    cell.backgroundColor = [UIColor blackColor];

//再次设置断点,可以看到我们的值又生效了,然后系统背景颜色被改变

UITableViewCell通过cell.backgroundColor设置背景颜色为什么没有效果

}

于是华丽丽的黑色背景cell就出现了。

UITableViewCell通过cell.backgroundColor设置背景颜色为什么没有效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值