简体   繁体   English

更改自定义tableViewCell的背景颜色

[英]change the background color of customize tableViewCell

I want to change my cell's background color 我想更改单元格的背景颜色

I see other question but they are not correspond my situation 我看到其他问题,但与我的情况不符

I want to do a notification tableView 我想做一个通知tableView

for example,If user have read cell, the cell's background color will change to white 例如,如果用户已读取单元格,则该单元格的背景颜色将变为白色

If not, the color is yellow 如果不是,则颜色为黄色

At beginning 开始时

I set color in 我设置颜色

-(void)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

   if(read[[indexPath row]])
     [cell.backView setBckgroundColor:[UIColor whiteColor];
   else
     [cell.backView setBckgroundColor:[UIColor redColor];
}

It works, and then i want to change color in 它有效,然后我想更改颜色

tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
   read[[indexPath row]] = yes;
   [cell.backView setBckgroundColor:[UIColor whiteColor];
   cell.name.text = @"test";
}

it works too 它也起作用

BUT if i selection other cell, it change to orignial color 但是如果我选择其他单元格,它将变为原始颜色

It seems it only can change ONE cell's color at same time 似乎只能同时更改一个单元格的颜色

No matter I use 不管我用

cell.backgroundColor
cell.contentView.backgroundColor
cell.backView 

it get the same result,can anyone help me 它得到相同的结果,有人可以帮我吗


edit 4/20 20:13 编辑4/20 20:13

I set read in 我开始读

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

sorry for misdirection,i update code 抱歉,我误导了我的代码

and after i selection other cell, i don't call [tableView reload] 在选择其他单元格后,我不会调用[tableView重新加载]

so i don't think this is the reason 所以我不认为这是原因

By the way, everything(eg label) can change but background color 顺便说一下,一切(例如标签)都可以改变,但是背景色

and if i select cell , it jump to other screen by navigation 如果我选择单元格,它会通过导航跳转到其他屏幕


Ans

conclusion first 结论第一

tableView: cellForRowAtIndexPath: is well

and

tableView: willDisplayCell: is well too

both of them can change background color 它们都可以改变背景颜色

but they execute when need to draw new cell or reload tableView 但是它们在需要绘制新单元格或重新加载tableView时执行

I still confuse why I can change label in didselectionRowAtIndexPath but i can't change color 我仍然感到困惑,为什么我可以在didselectionRowAtIndexPath中更改标签,但是我不能更改颜色

Change color using tableView: willDisplayCell: method 使用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];
    }
}

I think you want to set your variable "read" to YES in 我认为您想将变量“ read”设置为

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

and I guess you use only one variable to record the states just because you simplified it for posting here, otherwise you might want to record the state of each cell with separate variables. 我猜您仅使用一个变量来记录状态,只是因为您简化了该状态以便在此处发布,否则您可能希望使用单独的变量来记录每个单元格的状态。

You need to update the "read" property of the object at the index of your cell. 您需要在单元格的索引处更新对象的“读取”属性。

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

then: 然后:

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

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

Try to get the Selected cell in the didSelectRowAtIndexPath 尝试获取didSelectRowAtIndexPath中的Selected单元格

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

//set read property for the seleted index //设置索引的读取属性

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

} }

您是否在“ didSelectRowAtIndexPath”中为tableView Set读取属性调用reloadData并重新加载tableView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM