UITableViewCell选择了行的文本颜色更改 [英] UITableViewCell selected row's text color change

查看:87
本文介绍了UITableViewCell选择了行的文本颜色更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个tableview,我想知道如何更改所选行的文本颜色,比如Red?我试过这段代码:

I am having a tableview and I want to know that how can I change the selected row's text color, say to Red ? I tried by this code :

- (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)当我选择任何行时,文字颜色变为红色但是当我选择另一个时,之前选择的行的文本保持红色。我该怎么解决这个问题?

(1) When I select any row then text color changed to the red but when I select another then previously selected row's text remains red. how can I solve this ?

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

(2) When I scroll the table text color change to the black color how to solve this ?

谢谢..

推荐答案

tableView中执行此操作:cellForRowAtIndexPath:

cell.textLabel.highlightedTextColor = [UIColor redColor];

(并且不要使用 cell.text = ... 了。它已被推荐近两年了。请改用 cell.textLabel.text = ...

(And don't use cell.text = ... anymore. It has been deprecated for almost 2 years now. Use cell.textLabel.text = ... instead.)

正如 Raphael Oliveira 所述注释,如果您的单元格的selectionStyle等于 UITableViewCellSelectionStyleNone 这将无效。检查故事板以及选择样式。

As Raphael Oliveira mentioned in the comments, if the selectionStyle of your cell equals UITableViewCellSelectionStyleNone this won't work. Check Storyboard as well for the selection style.

这篇关于UITableViewCell选择了行的文本颜色更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆