【问题标题】:change the background color of customize tableViewCell更改自定义tableViewCell的背景颜色
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我想你想在

      中将你的变量“read”设置为 YES

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

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

      【讨论】:

      • 我这样做了,但它不起作用,我再次编辑代码谢谢
      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          尝试在 didSelectRowAtIndexPath 中获取 Selected 单元格

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

          //为选中的索引设置读取属性

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

          }

          【讨论】:

            【解决方案6】:

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

            【讨论】:

            • 我在 didSelectRowAtIndexPath 中设置了读取属性,但我没有重新加载 tableView
            猜你喜欢
            • 1970-01-01
            • 2020-03-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-06-23
            • 1970-01-01
            • 2017-11-04
            • 2011-09-12
            相关资源
            最近更新 更多