OGeek|极客世界-中国程序员成长平台

标题: ios - 如何恢复在 UITableViewCell 中所做的更改 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:50
标题: ios - 如何恢复在 UITableViewCell 中所做的更改

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

一切正常。除了一个 UIIssue。

当用户点击任何 cell 时,我想更改该特定 Cell 的文本颜色。当用户选择另一个时,我想要之前选择的 cell 应该恢复到原来的状态。

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

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

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

}

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



Best Answer-推荐答案


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

- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)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;
} 

关于ios - 如何恢复在 UITableViewCell 中所做的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33117420/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4