ios - UITableViewCell 选中行的文字颜色变化

我有一个表格 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 以及选择样式。

https://stackoverflow.com/questions/5841056/

相关文章:

ios - 禁用 UITextField 的自动更正

objective-c - 将 Xcode 组创建为文件系统文件夹的工作流程

iphone - UISegmentedControl 文本以编程方式

ios - '无效更新 : invalid number of rows in section 0

ios - 按字典中键的值对字典的 NSArray 进行排序

objective-c - 具有多个目标的 Objective C 到 Swift 头文件

ios - 如何使用图像和标签制作自定义 UIBarButtonItem?

ios - 如何在运行时更改约束优先级

iphone - 快速将单个引脚添加到 MKMapView?

ios - 如何检查 NSArray 中的对象是否为 NSNull?