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



在我的应用程序中,我想更改单元格文本颜色而不消失单元格分隔符。我正在使用以下代码

- (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;
}

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

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

- (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;
}
-(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设置为清晰的视图,不如简单地在选择时不允许突出显示单元格即可实现您想要的效果吗?它将防止单元格和分隔符根据选择自动更改,但您必须自己管理识别点击手势并突出显示标签文本。

从代码中删除selectedBackgroundView

然后,您需要在UITableViewDelegate中实现tableView:shouldHighlightRowAtIndexPath:

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

最新更新