更改自定义 tableViewCell 的背景颜色 [英] change the background color of customize tableViewCell

查看:34
本文介绍了更改自定义 tableViewCell 的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改单元格的背景颜色

I want to change my cell's background color

我看到其他问题,但它们与我的情况不符

I see other question but they are not correspond my situation

我想做一个通知tableView

I want to do a notification tableView

例如,如果用户读取了单元格,单元格的背景颜色将变为白色

for example,If user have read cell, the cell's background color will change to white

如果不是,则颜色为黄色

If not, the color is yellow

开始

我在

-(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";
}

它也有效

但如果我选择其他单元格,它会更改为原始颜色

BUT if i selection other cell, it change to orignial color

好像只能同时改变一个单元格的颜色

It seems it only can change ONE cell's color at same time

不管我用

cell.backgroundColor
cell.contentView.backgroundColor
cell.backView 

它得到了相同的结果,任何人都可以帮助我

it get the same result,can anyone help me

编辑 4/20 20:13

edit 4/20 20:13

我设置了读入

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

抱歉误导,我更新代码

在我选择其他单元格后,我不调用 [tableView reload]

and after i selection other cell, i don't call [tableView reload]

所以我不认为这是原因

顺便说一下,除了背景颜色外,所有东西(例如标签)都可以改变

By the way, everything(e.g label) can change but background color

如果我选择单元格,它会通过导航跳转到其他屏幕

and if i select cell , it jump to other screen by navigation

答案

先总结

tableView: cellForRowAtIndexPath: is well

tableView: willDisplayCell: is well too

两者都可以改变背景颜色

both of them can change background color

但它们在需要绘制新单元格或重新加载 tableView 时执行

but they execute when need to draw new cell or reload tableView

我仍然困惑为什么我可以在 didselectionRowAtIndexPath 中更改标签但我不能改变颜色

I still confuse why I can change label in didselectionRowAtIndexPath but i can't change color

推荐答案

使用 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];
    }
}

这篇关于更改自定义 tableViewCell 的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆