Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 更改自定义tableViewCell的背景色_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 更改自定义tableViewCell的背景色

Ios 更改自定义tableViewCell的背景色,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我想更改单元格的背景色 我看到了另一个问题,但它们不符合我的情况 我想做一个通知表视图 例如,如果用户已读取单元格,则单元格的背景色将变为白色 如果不是,则颜色为黄色 起初 我把颜色加进去了 -(void)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{ if(read[[indexPath row]]) [cell.backView setBckgroundColo

我想更改单元格的背景色

我看到了另一个问题,但它们不符合我的情况

我想做一个通知表视图

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

如果不是,则颜色为黄色

起初

我把颜色加进去了

-(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:13

我开始阅读

tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
对不起,我弄错了方向,我更新了代码

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

所以我不认为这是原因

顺便说一句,除了背景色之外,任何东西(例如标签)都可以改变

如果我选择了cell,它会通过导航跳转到另一个屏幕


Ans

结论一

tableView: cellForRowAtIndexPath: is well

它们都可以改变背景颜色

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

我仍然不明白为什么我可以在DidSelectionRowatineXpath中更改标签
但是我不能更改颜色

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

我认为您希望在中将变量“read”设置为YES

tableView:(UITableView*)tableView没有选择rowatindexpath:(nsindepath*)indepath

我猜您只使用一个变量来记录状态,因为您简化了它以便在此处发布,否则您可能希望使用单独的变量记录每个单元格的状态。

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

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


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

尝试获取didSelectRowAtIndexPath中的选定单元格

-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath {

//设置所选索引的读取属性

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

}

您是否为tableView调用了重新加载数据
在“didSelectRowAtIndexPath”中设置read属性并重新加载tableView

我这样做但不起作用,我再次编辑代码感谢在didSelectRowAtIndexPath中设置read属性但我不重新加载tableView
-(void)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
   if(currentObjectForThisCell.read)
     [cell.backView setBckgroundColor:[UIColor whiteColor];
   else
     [cell.backView setBckgroundColor:[UIColor redColor];
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];