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

标签 ios objective-c iphone uitableview

我有一个表格 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/

相关文章:

iphone - UIWebView背景设置为Clear Color,但不透明

ios - 想要在 cloudkit 仪表板上上传视频并在 ios 应用程序中显示该视频?

使用 AVAudioPlayer 播放音频文件时 iPhone 崩溃且来电中断播放

iphone - 如何以编程方式完全像这个 UIButton 布局?

iphone - 适用于 iOS 的 RTSP 视频流

ios - 使用 AVMutableVideoComposition 导出 CALayer

ios - Swift iOS 自动调整 UITableViewCell() 的 UILabel 高度

ipad - 嵌入在 UINavigationBar/Item 中的 UISegmentedControl

iphone - addSubview 奇怪的效果

iphone - 关于高分持久性的建议(iPhone、Cocoa Touch)