您的位置:首页 > 产品设计 > UI/UE

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

2015-07-29 17:38 519 查看
在做ios6的适配的时候发现,无论我是把cell的backgroundColor设置颜色还是增加一个view设置背景色,在cell上都没有效果。

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

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

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

用cell.backgroundColor = [UIColor blueColor];

设置背景没有效果。

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





查字典的结果:

有一个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的背景颜色是已经修改了的。





return cell;

}

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

{

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





cell.backgroundColor = [UIColor blackColor];

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





}

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



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: