我有一个表格 View ,我想知道如何更改所选行的文本颜色,比如 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) 当我滚动表格文本颜色变为黑色时如何解决?
谢谢..
最佳答案
在 tableView:cellForRowAtIndexPath:
:
cell.textLabel.highlightedTextColor = [UIColor redColor];
(并且不要再使用 cell.text = ...
了。它已经被弃用了将近 2 年。使用 cell.textLabel.text = ...
代替。)
作为 Raphael Oliveira评论中提到,如果您的单元格的 selectionStyle 等于 UITableViewCellSelectionStyleNone
这将不起作用。检查 Storyboard 以及选择样式。
关于ios - UITableViewCell 选中行的文字颜色变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5841056/