【发布时间】:2015-04-20 09:59:26
【问题描述】:
我想更改单元格的背景颜色
我看到其他问题,但它们不符合我的情况
我想做一个通知tableView
例如,如果用户已阅读单元格,则单元格的背景颜色将变为白色
如果不是,颜色为黄色
开始
我设置了颜色
-(void)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
if(read[[indexPath row]])
[cell.backView setBckgroundColor:[UIColor whiteColor];
else
[cell.backView setBckgroundColor:[UIColor redColor];
}
它工作,然后我想改变颜色
tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
read[[indexPath row]] = yes;
[cell.backView setBckgroundColor:[UIColor whiteColor];
cell.name.text = @"test";
}
它也有效
但是如果我选择其他单元格,它会变成原来的颜色
似乎只能同时改变一个单元格的颜色
不管我用什么
cell.backgroundColor
cell.contentView.backgroundColor
cell.backView
结果一样,谁能帮帮我
编辑 4/20 20:13
我设置了读取
tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
抱歉误导,我更新代码
在我选择其他单元格后,我不调用 [tableView reload]
所以我不认为这是原因
顺便说一句,一切(例如标签)都可以改变,但背景颜色
如果我选择单元格,它会通过导航跳转到其他屏幕
回答
结论第一
tableView: cellForRowAtIndexPath: is well
和
tableView: willDisplayCell: is well too
它们都可以改变背景颜色
但它们在需要绘制新单元格或重新加载 tableView 时执行
我仍然很困惑为什么我可以更改 didselectionRowAtIndexPath 中的标签 但我不能改变颜色
【问题讨论】:
标签: ios objective-c uitableview