Ios 如何在不隐藏分隔符的情况下更改单元格文本颜色?

Ios 如何在不隐藏分隔符的情况下更改单元格文本颜色?,ios,uitableview,Ios,Uitableview,在我的应用程序中,我想更改单元格文本的颜色,而不丢失单元格分隔符 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = nil; static NSString *identifier = @"cell"; cell = [tableView dequeueReusabl

在我的应用程序中,我想更改单元格文本的颜色,而不丢失单元格分隔符

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"cell";
    cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

        UIView * selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
        [selectedBackgroundView setBackgroundColor:[UIColor clearColor]]; // set color here
        [cell setSelectedBackgroundView:selectedBackgroundView];

        cell.backgroundColor=[UIColor clearColor];
        cell.textLabel.highlightedTextColor = [UIColor redColor];
        [cell setOpaque:NO];

    }
    cell.textLabel.text=[contentArray objectAtIndex:indexPath.row];
    return cell;
}

但是当我点击单元格时,单元格分隔符也消失了?如何在不隐藏分隔符的情况下更改文本颜色?

与其将单元格的
selectedBackgroundView
设置为清晰视图,不如干脆不允许在选中时突出显示单元格以实现所需功能?这将防止单元格和分隔符根据选择自动更改,但您必须自己识别轻触手势并高亮显示标签文本

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   for(id view in cell.containtView.subview) {
     if([view isKindaOfClass:[UILabel class]])
        UILabel* titleLabel = (UILabel*)view;
        [titleLabel setTextColor:[UIColor whiteColor]]; // any you want
   }
}
从代码中删除
selectedBackgroundView

然后需要在
UITableViewDelegate
中实现
tableView:shoulldhighlightrowatindexpath:

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO; // do what's appropriate based on the indexPath
}

看来细胞分离器对很多人来说都是个问题。因此,我想说,与其按照我的建议禁用选择,不如将单元格分隔符设置为“无”,并在背景和选定背景视图中自己管理分隔符:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // ...

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"cell";
    cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

        UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
        [selectedBackgroundView setBackgroundColor:[UIColor clearColor]];
        [cell setSelectedBackgroundView:selectedBackgroundView];

        UIView *backgroundView = [[UIView alloc] initWithFrame:cell.frame];
        [backgroundView setBackgroundColor:[UIColor clearColor]];
        [cell setBackgroundView:backgroundView];

        UIView *selectedBackgroundSeparator = [[UIView alloc] initWithFrame:CGRectMake(tableView.separatorInset.left, cell.frame.size.height - 1, cell.frame.size.width - tableView.separatorInset.left, 1)];
        UIView *backgroundSeparator = [[UIView alloc] initWithFrame:selectedBackgroundSeparator.frame];

        selectedBackgroundSeparator.backgroundColor = backgroundSeparator.backgroundColor = tableView.separatorColor;

        [selectedBackgroundView addSubview:selectedBackgroundSeparator];
        [backgroundView addSubview:backgroundSeparator];

        cell.textLabel.highlightedTextColor = [UIColor redColor];
        [cell setOpaque:NO];

    }
    cell.textLabel.text=[contentArray objectAtIndex:indexPath.row];
    return cell;
}

或者,您可以使用默认的单元格分隔符,只需将自己的顶部和底部分隔符添加到
selectedBackgroundView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"cell";
    cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

        UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
        [selectedBackgroundView setBackgroundColor:[UIColor clearColor]];
        [cell setSelectedBackgroundView:selectedBackgroundView];

        UIView *topSelectedBackgroundSeparator = [[UIView alloc] initWithFrame:CGRectMake(tableView.separatorInset.left, 0, cell.frame.size.width - tableView.separatorInset.left, 1)];
        UIView *selectedBackgroundSeparator = [[UIView alloc] initWithFrame:CGRectOffset(topSelectedBackgroundSeparator.frame, 0, cell.frame.size.height)];

        topSelectedBackgroundSeparator.backgroundColor = selectedBackgroundSeparator.backgroundColor = tableView.separatorColor;

        [selectedBackgroundView addSubview:selectedBackgroundSeparator];
        [selectedBackgroundView addSubview:topSelectedBackgroundSeparator];

        cell.textLabel.highlightedTextColor = [UIColor redColor];
        [cell setOpaque:NO];

    }
    cell.textLabel.text=[contentArray objectAtIndex:indexPath.row];
    return cell;
}

抱歉,您的问题不清楚,我的意思是如何以及何时更改文本颜色?@Retro单击单元格集
tableView.separatorStyle=UITableViewCellSeparatorStyleNone时,我想更改文本颜色在单元格中添加自定义行确保selectedBackgroundView可能隐藏分隔符..只需注释掉这一行,然后尝试[cell setSelectedBackgroundView:selectedBackgroundView];谢谢你的回复。但是当我使用上面的代码时,突出显示的文本颜色没有显示?