【问题标题】:Change text color in UITableViewCell following event happened发生事件后更改 UITableViewCell 中的文本颜色
【发布时间】:2016-12-07 13:22:19
【问题描述】:

我有包含视频节目列表的表格视图。当我点击一个程序时,该程序将在该表视图上方的播放器视图中播放。看起来像这样。

我想要做的是在我点击一行后我希望该行中的文本变成红色。我的问题是它不能立即变成红色,但我必须滚动该行的表格视图才能离开屏幕并再次回到屏幕上,然后该选定行中的文本将变为红色。我知道这是 tableview cellForRowAtIndexPath 的行为。所以我在 tableview didSelectRowAtIndexPath 中添加了[self.tableView reloadData],但它不起作用。正如我所说,我必须滚动该选定的滚动屏幕,然后返回屏幕以更改其颜色。我该如何解决这个问题?

下面是相关代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   static NSString *CellIdentifier = @"LiveTableViewCell";

   LiveTableViewCell *cell = (LiveTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   //...... (more stuff here but not related)

   //Color - for one selected/playing
   if (programList.programId == self.liveData.programId ) {
      [str1 addAttribute:NSStrikethroughColorAttributeName value:[CustomColor textColorf45160] range:strRange1]; //red color
      [str1 addAttribute:NSForegroundColorAttributeName value:[CustomColor textColorf45160] range:strRange1]; //red color
    }

    cell.title.attributedText = str1;

    return cell;
}

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

   //...... (more stuff here but not related)
   [self.tableView reloadData];  
}

【问题讨论】:

  • 据我所知,您没有通过检查其 indexPath 来设置单元格颜色,而是使用您定义和更新的一些其他值。更新这些 ID 的代码可以在 tableView didSelectRowAtIndexPath 之后运行吗?

标签: ios objective-c uitableview


【解决方案1】:

最简单的做法是将配置单元的逻辑提取到它自己的方法中。然后你的 cellForRowAtIndexPath 和 didSelect 方法可以调用同一个方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   static NSString *CellIdentifier = @"LiveTableViewCell";

   LiveTableViewCell *cell = (LiveTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   //...... (more stuff here but not related)
   [self configureCell:cell withProgramList:programList];

    return cell;
}

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

   //...... (more stuff here but not related)

   LiveTableViewCell *cell = (id)[tableView cellForRowAtIndexPath:indexPath];
   ProgramList *programList = // get programList at indexpath
   [self configureCell:cell withProgramList:programList];
}

- (void)configureCell:(LiveTableViewCell *)cell withProgramList:(ProgramList *)programList {
   //Color - for one selected/playing
   if (programList.programId == self.liveData.programId ) {
      [str1 addAttribute:NSStrikethroughColorAttributeName value:[CustomColor textColorf45160] range:strRange1]; //red color
      [str1 addAttribute:NSForegroundColorAttributeName value:[CustomColor textColorf45160] range:strRange1]; //red color
    }

    cell.title.attributedText = str1;
}

【讨论】:

    【解决方案2】:

    您的“self.tableView”应该与“didSelectRowAtIndexPath”中的“tableView”相同。

    【讨论】:

      【解决方案3】:

      你可以使用 - (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath 委托函数如下:

      (void)tableView:(UITableView *)tableView 
      didHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
      
        LiveTableViewCell *cell = (LiveTableViewCell *)[tableView    cellForRowAtIndexPath:indexPath];
        cell.title.textColor = UIColor.redColor;
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-13
        • 1970-01-01
        • 2016-01-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多