Ios UITableViewCell选定行的文本颜色更改

Ios UITableViewCell选定行的文本颜色更改,ios,objective-c,iphone,uitableview,Ios,Objective C,Iphone,Uitableview,我有一个tableview,我想知道如何更改所选行的文本颜色,比如说红色?我尝试了以下代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell= [[[UITableViewCell all

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

- (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.text=。。。不再它已经被弃用了将近两年了。使用cell.textlab.text=。。。相反


如评论中所述,如果单元格的selectionStyle等于UITableViewCellSelectionStyleNone,则此选项无效。如果只想更改文本颜色,而不想更改单元格背景颜色,请检查故事板的选择样式。

。您可以使用此方法。在CellForRowatineXpath方法中编写此代码

UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = selectionColor;
cell.textLabel.highlightedTextColor = [UIColor redColor];

我也有同样的问题,试试这个

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    for (id object in cell.superview.subviews) {
        if ([object isKindOfClass:[UITableViewCell class]]) {
            UITableViewCell *cellNotSelected = (UITableViewCell*)object;
            cellNotSelected.textLabel.textColor = [UIColor blackColor];
        }
    }

    cell.textLabel.textColor = [UIColor redColor];

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

这可能是您和我的问题的解决方案。

如果您已经对UITableViewCell进行了子类化,那么在awakeFromNib方法中设置颜色会更容易/更干净,假设您是从故事板或xib实例化的

@implementation MySubclassTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame];
    self.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:0.1 green:0.308 blue:0.173 alpha:0.6];
    self.customLabel.highlightedTextColor = [UIColor whiteColor];
}

@end

颜色应该是红色,直到我选择另一行!值得一提的是,如果单元格的selectionStyle等于UITableViewCellSelectionStyleNone,则此选项将不起作用。您可能没有添加此代码,但请签入序列图像板,以避免像我一样浪费大量时间试图找出此方法不起作用的原因。我使用此方法,但在iOS 10上设置selectedBackgroundView颜色会导致表格分隔符在选择时消失。我仍在研究一个很好的解决这个副作用的方法。
@implementation MySubclassTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame];
    self.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:0.1 green:0.308 blue:0.173 alpha:0.6];
    self.customLabel.highlightedTextColor = [UIColor whiteColor];
}

@end