2015-04-20 99 views
1

我想改变我的单元格背景色更改自定义背景色tableViewCell

我看其他的问题,但他们不符合我的情况

我想做一个通知的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]

,所以我不认为这是什么原因

顺便说一句,一切都(E 。克标签)可以改变,但背景颜色

,如果我选择小区时,它通过导航


答跳转到其他屏幕

结论第一

tableView: cellForRowAtIndexPath: is well 

tableView: willDisplayCell: is well too 

他们都可以改变背景颜色

但执行时需要制定新的细胞或重装的tableView

我还是搞不清为什么我可以在didselectionRowAtIndexPath 更改标签,但我不能改变颜色

回答

2

更改颜色使用tableView: willDisplayCell:方法

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (...){ //do your stuff. 
     [cell.backView setBckgroundColor:[UIColor redColor]; 
    } else { 
     [cell.backView setBckgroundColor:[UIColor whiteColor]; 
    } 
} 
0

我想你想设置你的变量

“读”为YES

tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath

我想你只使用一个变量来记录状态,只是因为你简化它在这里发布,否则你可能想用单独的变量记录每个单元格的状态。

+0

我这样做,但它不起作用,我再次编辑代码感谢 – jim

0

您需要更新单元索引处对象的“读取”属性。

tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath { 
    [cell.backView setBckgroundColor:[UIColor whiteColor]; 
    currentObjectForThisCell.read = YES; 
} 

则:

-(void)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { 
    if(currentObjectForThisCell.read) 
    [cell.backView setBckgroundColor:[UIColor whiteColor]; 
    else 
    [cell.backView setBckgroundColor:[UIColor redColor]; 
} 
+0

我设置了读取但它不工作,我再次编辑代码感谢 – jim

0

在你编辑的代码,当您选择单元格,设置读取标志YES和改变颜色为白色,这很好,但在你的cellForRowAtIndexPath:设置单元格的读标志YES为红色,NO为白色,这与didSelectRowAtIndexPath:方法中的行为相反。

+0

对不起,这是我的错误,再次编辑 – jim

0

试图让选定单元格中的didSelectRowAtIndexPath方法

- (空)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath {

//设置读取你选择的指数财产

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
cell.backgroundColor = [UIColor redColor]; 

}

0

你打电话reloadData了的tableView 集合R ead属性在“didSelectRowAtIndexPath”并重新加载tableView

+0

我在didSelectRowAtIndexPath中设置读取属性,但我不重新加载tableView – jim