【问题标题】:How to revert back the changes made in UITableViewCell如何恢复在 UITableViewCell 中所做的更改
【发布时间】:2016-01-12 01:47:30
【问题描述】:

我有一个名为Hobbies 的自定义UITableViewCell

一切正常。除了一个 UIIssue。

当用户点击任何cell 时,我想更改该特定Cell 的文本颜色。当用户选择另一个时,我希望之前选择的cell 应返回其原始状态。

目前我可以更改 Cell 选择的颜色,但当用户选择另一个时无法将其还原。

这是我用来更改特定单元格的textColor 的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 HobbiesCell *cell = (HobbiesCell*)[tableView cellForRowAtIndexPath:indexPath]; 
cell.dateLabel.textColor = [UIColor colorWithWhite:255 alpha:0.5f];

}

当用户选择另一个单元格时如何将其还原。

【问题讨论】:

标签: ios objective-c uitableview tableviewcell


【解决方案1】:

试试 cell.textLabel.highlightedTextColor

    if (cell == nil) {

        ........
         /* your cell initiation code */
        ........

        cell.textLabel.textColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.highlightedTextColor = [UIColor orangeColor];
    }

【讨论】:

  • 我在哪里写过这个方法?拉什米
  • 在你的 cellForRowAtIndexPath: 委托方法中!!试试看吧。
  • 是的!我是印度人。
  • 我们可以谈谈吗:) @Rashmi
  • 要么你在这里创建一个聊天室,要么我 :)
【解决方案2】:

您需要维护对当前选定 indexPath 的引用。有点像……

@property (nonatomic, strong) NSIndexPath *currentHobby;

然后在你的didSelectRowAtIndexPath: 方法中插入这个...

if(_currentHobby && ![_currentHobby isEqual:indexPath]) {
    HobbiesCell *currentCell = [tableView cellForRowAtIndexPath:_currentHobby];
    currentCell.dateLabel.textColor = [UIColor originalColor];
}
_currentHobby = indexPath;

然后确保在您的 cellForRowAtIndexPath: 中包含...

if([indexPath isEqual:_currentHobby]) {
    cell.dateLabel.textColor = [UIColor selectedColor];
} else { 
    cell.dateLabel.textColor = [UIColor originalColor];
}

【讨论】:

    【解决方案3】:

    如果你要重新加载表格视图,那么试试这个。

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
      // reload the table 
      [tableView reloadData];
     HobbiesCell *cell = (HobbiesCell*)[tableView cellForRowAtIndexPath:indexPath];
    previousCell.dateLabel.textColor = [UIColor colorWithWhite:255 alpha:0.5f]; // color insert which you want to insert 
    }
    

    希望它在不添加变量的情况下对您有所帮助。

    【讨论】:

      【解决方案4】:

      您应始终牢记,单元格是可重复使用的,因此您更改的单元格将按原样用于在滚动时显示其他行。

      相反,您应该保留一组您自己的模型来保存数据(在您的情况下为颜色信息)并仅使用单元格来显示它。

      要恢复颜色,只需保留对最新NSIndexPath 的引用。

      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
      {
           MyRowModel *prevRowModel = [self.rowModels objectAtIndex:self.lastIndexPath.row];
           prevRowModel.color = [UIColor colorWithWhite:255 alpha:1f];
      
           MyRowModel *rowModel = [self.rowModels objectAtIndex:indexPath.row];
           rowModel.color = [UIColor colorWithWhite:255 alpha:0.5f]; 
      
           [self.tableView reloadRowsAtIndexPaths:@[indexPath, self.lastIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
           self.lastIndexPath = indexPath;
      }
      

      【讨论】:

        【解决方案5】:

        声明这个变量

        int selectedIndex;
        

        在你的 cellForRowAtIndexPath 中

        if(indexPath.row == selectedIndex)
        {
        cell.dateLabel.textColor = [UIColor colorWithWhite:255 alpha:0.5f];
        }
        else
        {
            cell.dateLabel.textColor = [UIColor colorWithWhite:0 alpha:0.5f];//your default cell color
        }
        

        在你的 didSelectRowAtIndex 中

        selectedIndex = indexPath.row;
        [tableView reloadData];
        

        【讨论】:

        • 这里假设tableView只有一个部分
        【解决方案6】:

        您可以创建一个 UITableViewCell 对象,例如 previousCell,并在每次选择时分配它。这将是您最后选择的单元格,您可以在每次单击新单元格时为其指定默认颜色。

        - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        {
            HobbiesCell *cell = (HobbiesCell*)[tableView cellForRowAtIndexPath:indexPath];
            previousCell.dateLabel.textColor = [UIColor blackColor]; //Assuming that this is the color you want to go back 
            cell.dateLabel.textColor = [UIColor colorWithWhite:255 alpha:0.5f];
            previousCell = cell;
        } 
        

        【讨论】:

        • 我不鼓励持有对单元格本身的引用,因为它可能会被重复使用。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-19
        • 1970-01-01
        • 2016-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多