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

时间:2011-08-10 16:38:31

标签: objective-c ios uitableview background-color

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

enter image description here

如何为整个细胞着色?我的实现如下。

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

//Clear colors for labels
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];

7 个答案:

答案 0 :(得分:12)

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

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

答案 1 :(得分:1)

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

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

答案 2 :(得分: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;
}


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

答案 3 :(得分:0)

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

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

希望这有助于某人

答案 4 :(得分: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];
    }
}

答案 5 :(得分:0)

的UIImageView

UIImageView右侧有一个V形图像。最好是在故事板中,但可以通过将其附加到内容视图来轻松地在代码中进行管理。

答案 6 :(得分:-1)

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

cell.accessoryView.backgroundColor = [UIColor clearColor];

或:

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