我有一个表格视图,我想知道如何更改所选行的文本颜色,例如 Red ?我尝试通过这段代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell= [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
cell.text = [localArray objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
cityName = [localArray objectAtIndex:indexPath.row];
UITableViewCell* theCell = [tableView cellForRowAtIndexPath:indexPath];
theCell.textColor = [UIColor redColor];
//theCell.textLabel.textColor = [UIColor redColor];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
(1) 当我选择任何行时,文本颜色更改为红色,但当我选择另一个时,先前选择的行的文本保持红色。我该如何解决这个问题?
(2) 当我滚动表格文本颜色变为黑色时如何解决这个问题?
Thanks..
执行此操作tableView:cellForRowAtIndexPath:
:
cell.textLabel.highlightedTextColor = [UIColor redColor];
(并且不要使用cell.text = ...
不再了。它已被弃用近 2 年了。使用cell.textLabel.text = ...
反而。)
As 拉斐尔·奥利维拉 https://stackoverflow.com/users/1792327/raphael-oliveira评论中提到,如果您的单元格的选择样式等于UITableViewCellSelectionStyleNone
这是行不通的。还要检查故事板以了解选择样式。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)