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就出现了。
查了一下,找到如下文章,解决了这个问题。
链接: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就出现了。
相关文章推荐
- How do I sort an array of hashes by a value in the hash?
- VS2008 LINK : fatal error LNK1000: Internal error during IncrBuildImage
- UITouch 触摸事件处理(实例)
- ios学习笔记:设置UIButton的文字显示位置、字体的大小、字体的颜色
- hdu 2818 Building Block
- win10 Build 9926更新出现错误代码0x80240020的解决方法
- UIView
- [LeetCode][Java] Longest Consecutive Sequence
- continue和break的使用注意
- Codeforces Round #312 (Div. 2) D. Guess Your Way Out! II (求区间的补、交)
- UISearchController
- iOS5中UIViewController的新方法
- 创建servlet类时出现的“private static final long serialVersionUID = 1L;”语句是什么意思啊?
- UIScrollView下拉刷新效果实现
- 如何重建 wf_java_deferred 队列(queue)
- UITableViewCell滑动删除及移动
- SQL Server 2012 SEQUENCE 使用
- UIImageView设置手势无效
- easyui-combobox下拉多选
- 短时间大量connect()返回errno 99: cannot assign request address