带附件的UITableViewCell

时间:2012-03-28 08:40:15

标签: ios xcode uitableview

我有一个使用UITableView的应用程序。每个细胞都有配件。但我不能设置backgroundColor ti这个单元格。

enter image description here

我尝试以这种方式设置backgrounColors:

cell.contentView.backgroundColor =[UIColor redColor];
cell.backgroundView.backgroundColor = [UIColor redColor];
cell.backgroundColor = [UIColor redColor];

但只有contentView工作。我该怎么做?日Thnx

1 个答案:

答案 0 :(得分:2)

第二种方式cell.backgroundView.backgroundColor = [UIColor redColor];并没有错。不幸的是,在你创建一个后,没有backgroundView。您必须创建一个UIView并将其设置为单元格的背景。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"];
    UIView *background = [[UIView alloc] initWithFrame:CGRectZero];
    background.backgroundColor = [UIColor redColor];
    cell.backgroundView = background;
}
相关问题