设置UITableViewCell的背景颜色

9

我找了很多解决方案,想要将 accessoryView 的背景颜色设置为与 cell 的 contentView 相同的背景颜色。

    cell.contentView.backgroundColor = [UIColor colorWithRed:178/255.f green:14/255.f blue:12/255.f alpha:0.05];
    cell.accessoryView.backgroundColor =[UIColor colorWithRed:178/255.f green:14/255.f blue:12/255.f alpha:0.05];

有一个解决方案可以使用,但只允许我为所有单元格使用一种颜色。

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

不使用accessoryView,改用图像是唯一的解决方案吗?

谢谢!


答案就是cell.backgroundColor...当然,当你在cell类内部时,它就是self.backgroundColor。但要注意分离的contentView,在storyboard中可能已经设置了默认的背景颜色,它会覆盖你的单元格颜色!!! - Fattie
6个回答

28
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.backgroundColor = [UIColor greenColor];
}

使用此方法UITableViewDelegate,可以将单元格的颜色设置为不同的颜色。请注意,Apple明确建议您在文档中的tableView:willDisplayCell:forRowAtIndexPath:方法中更改backgroundColor属性:

如果要更改单元格的背景颜色,请在表视图委托的tableView:willDisplayCell:forRowAtIndexPath:方法中执行此操作。

事实上,在iOS 6中,从任何其他位置(如tableView:cellForRowAtIndexPath:方法)对该属性的更改都不会产生任何效果。 在iOS 7中不再是这种情况,但是Apple修改该属性的建议仍然是在tableView:willDisplayCell:ForRowAtIndexPath:中进行(没有任何解释)。
对于交替颜色,请执行以下示例操作:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row % 2) {
        cell.backgroundColor = [UIColor yellowColor];
    } else {
        cell.backgroundColor = [UIColor redColor];    
    }
}

这里有一个相当晦涩的解释(链接在此):如果委托选择实现此方法,则可以在显示单元格对象之前对其进行最后一刻更改。使用此方法,委托应仅更改先前由表视图设置的基于状态的属性,例如选择和背景颜色,而不是内容。 - Edoardo
...对我来说,更多的是关于在willDisplayCell中不设置内容,而不是相反。 - Edoardo

19

我也曾经为这个问题苦苦挣扎,只能通过创建带附件的自定义图像来解决它。但是我刚刚找到了一个既有效又不需要自定义图像的解决方案。诀窍在于更改单元格的backgroundView颜色而不是backgroundColor。

UIView *myView = [[UIView alloc] init];
if (indexPath.row % 2) {
    myView.backgroundColor = [UIColor whiteColor];
} else {
    myView.backgroundColor = [UIColor blackColor];
}
cell.backgroundView = myView;

无需更改accessoryView或contentView的背景颜色。它们会自动跟随。


2014年的注释。通常情况下,您会使用-(void)setSelected:(BOOL)selected animated:(BOOL)animated

因此,您将拥有一个自定义单元格类,并像这样设置普通/选定的颜色...

HappyCell.h
@interface HappyCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *mainLabel;
etc...
@end

HappyCell.m
@implementation HappyCell

-(id)initWithStyle:(UITableViewCellStyle)style
      reuseIdentifier:(NSString *)reuseIdentifier
    {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
        {
        }
    return self;
    }

-(void)awakeFromNib
    {
    }

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
    {
    [super setSelected:selected animated:animated];

    if(selected)
        {
        self.backgroundColor = [UIColor redColor];
        .. other setup for selected cell
        }
    else
        {
        self.backgroundColor = [UIColor yellowColor];
        .. other setup for normal unselected cell
        }
    }

@end

// to help beginners.......
// in your table view class, you'd be doing this...

-(NSInteger)tableView:(UITableView *)tableView
      numberOfRowsInSection:(NSInteger)section
    {
    return yourDataArray.count;
    }

-(UITableViewCell *)tableView:(UITableView *)tv
      cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    NSInteger thisRow = indexPath.row;
    ContentsCell *cell = [tv
      dequeueReusableCellWithIdentifier:@"cellName"
      forIndexPath:indexPath]; 
    // "cellName" must be typed in on the cell, the storyboard
    // it's the "identifier", NOT NOT NOT the restorationID

    [cell setupForNumber: thisRow];
    cell.mainLabel.text = yourDataArray[ thisRow ][@"whatever"];
    cell.otherLabel.text = yourDataArray[ thisRow ][@"whatever"];

    return cell;
    }

希望能对某人有所帮助。


7
这对我有效:

这个方法适用于我:

cell.contentView.backgroundColor = [UIColor darkGreyColor];

2

对于所有颜色相同的行

cell.backgroundColor = [UIColor colorWithRed:6.0/255.0 green:122.0/255.0 blue:145.0/255.0 alpha:1.0f];

针对2种颜色

cell.backgroundColor = [UIColor colorWithRed:6.0/255.0 green:122.0/255.0 blue:145.0/255.0 alpha:1.0f];

    if ((cell.backgroundColor = (indexPath.row % 2 == 0 ? [UIColor colorWithRed:6.0/255.0 green:122.0/255.0 blue:145.0/255.0 alpha:1.0f] : [UIColor colorWithRed:2.0/255.0 green:68.0/255.0 blue:80.0/255.0 alpha:1.0f]))){
        cell.textLabel.textColor = [UIColor whiteColor];

}

0

对于其他可能会遇到此问题并想将其UITableViewCell背景设置为图案或纹理而不是纯色的人,您可以像这样操作:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"pattern.png"]];
}

-1

如果想要拥有不同的背景和其他效果,最好的选择可能是创建自己的TableViewCell实现,在其中可以根据内容或索引等放置显示任何您想要的逻辑。


这个问题同样适用于自定义/子类化的UITableViewCells。 - paiego

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接