目标C:如何为整个细胞添加颜色(带有披露指标)

问题描述 投票:10回答:7

我正在尝试为我的单元格添加背景颜色,但无法使用披露指示器为该部分着色(请参见下面的屏幕截图)

我可以做些什么来为整个细胞着色?我的实现如下。

cell.contentView.backgroundColor = [UIColor colorWithHexString:@"#FFFFCC"];

//Clear colors for labels
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
objective-c ios uitableview background-color
7个回答
12
投票

UITableViewCell子类UIView;您可以像设置任何其他视图一样设置其背景颜色。 “分组”式表格视图中的单元格需要更多工作,但它看起来并不像您正在使用它,因此:

cell.backgroundColor = [UIColor colorWithHexString:@"#FFFFCC"];

1
投票

我的代码使用(在单元格中)self.backgroundColor,所以你应该使用cell.backgroundColor而不是cell.contentView.backgroundColor。 contentView不包括我猜想的选择指标视图;)

如果您只想在选中时更改颜色,请使用selectedBackgroundView.backgroundColor


0
投票

使用与单元格相同的边界着色uiview并将其添加为子视图




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    UIView *colorView = [cell viewWithTag:200];
    if(!colorView){
        colorView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 44)] autorelease];
        colorView.backgroundColor = [UIColor redColor];
        colorView.tag = 200;
        [cell addSubview:colorView];
    }

    return cell;
}


然后将所有子视图添加到该彩色视图中。


0
投票

接受的答案对我不起作用,但以下代码有效:

[cell.contentView setBackgroundColor:[UIColor whiteColor]];

希望这有助于某人


0
投票

这让我疯了。我在cellForRowAtIndexPath方法中尝试了许多不同的方法。我终于找到了这样做的方法是覆盖willDisplayCell方法,如下所示:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row % 2) {
        cell.backgroundColor = [UIColor whiteColor];
    } else {
        cell.backgroundColor = [UIColor colorWithRed:225.0/255.0 green:225.0/255.0 blue:225.0/255.0 alpha:1.0];
    }
}

0
投票

UIImageView

UIImageView右侧有一个V形图像。优选地在故事板中,但是通过将其附加到内容视图,可以容易地在代码中管理它。


-1
投票

作为它的UIView子类,你应该能够设置accessoryView的背景颜色:

cell.accessoryView.backgroundColor = [UIColor clearColor];

要么:

cell.accessoryView.backgroundColor = [UIColor colorWithHexString:@"#FFFFCC"];
© www.soinside.com 2019 - 2024. All rights reserved.