如何更改单元格文本颜色而不隐藏分隔符?

时间:2013-12-21 05:11:42

标签: ios uitableview

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

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

但是当我点击细胞时,细胞分离器也在消失?如何在不隐藏分隔符的情况下更改文本颜色?

3 个答案:

答案 0 :(得分:1)

对于很多人来说,细胞分离器似乎是一个问题。所以,我会说,而不是按照我的建议禁用选择,将单元格分隔符设置为“无”并在背景和选定的背景视图中自己管理分隔符会更容易:

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

不是将单元格的selectedBackgroundView设置为清晰视图,而是在选择完成所需内容时不允许单元格突出显示?它会阻止单元格和分隔符根据选择自动更改,但您必须自己管理识别点击手势并突出显示标签文本。

从代码中删除selectedBackgroundView

然后,您需要在tableView:shouldHighlightRowAtIndexPath:

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