我有一个tableview,我想知道如何更改所选行的文本 colored颜色 ,比如红色?我try 了以下代码:

- (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) 当我 Select 任何一行时,文本 colored颜色 变为红色,但当我 Select 另一行时,之前 Select 的行的文本仍为红色.我该怎么解决这个问题?

(2) 当我滚动表格文字 colored颜色 变为黑色时,如何解决这个问题?

谢谢

推荐答案

tableView:cellForRowAtIndexPath:分钟内做到这一点:

cell.textLabel.highlightedTextColor = [UIColor redColor];

(不要再使用cell.text = ...了.它已经被弃用了将近两年了.改用cell.textLabel.text = ....)


正如 comments 中提到的Raphael Oliveira,如果单元格的selectionStyle等于UITableViewCellSelectionStyleNone,这将不起作用.也可以查看故事板中的 Select 样式.

Objective-c相关问答推荐

在 iOS 中检测 PAN 手势的方向

如何可靠地检测 iOS 9 上是否连接了外部键盘?

UITableViewCell 突出显示时使标签的背景清晰

CABasicAnimation 无 HUGE_VALF 无限重复?

Interface Builder 中的 IBOutletCollection 集排序

当单元格全屏时,为什么 UICollectionView 会记录错误?

使用情节提要的条件转场

在 Cocoa 中你更喜欢 NSInteger 还是 int,为什么?

对块进行类型定义是如何工作的

使用 DatePicker 展开和折叠 UITableViewCells

如何在嵌入 UIViewController 的 UITableView 中直观地创建和使用静态单元格

在我滚动之前,数据不会加载到 UITableView 中

UIScrollView - 显示滚动条

检测平移手势结束

Objective-c 2.0 中的可选参数?

当 UIView 框架更改时,视图内的 AVPlayer 层不会调整大小

AppDelegate 的当前视图控制器?

如何将 tintColor 应用于 UIImage?

如何判断 WkWebView 是否在 Objective-C 中完成加载?

将浮点数四舍五入到objective C中的下一个整数?