本文介绍了设置 UITableViewCell 的背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾四周,找到了一个解决方案,将附件视图的背景颜色设置为与单元格的内容视图相同的背景颜色.

I have looked around to find a solution for setting the background color of the accessoryView to the same background color as the cell´s 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];

有一个可行的解决方案,但只让我对所有单元格使用一种颜色.

There is a solution that works but only let me use one color for all cells.

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

不使用附件视图而是使用图像的唯一解决方案是?

Is the only solution to not use the accessoryView and use an image instead?

谢谢!

推荐答案

我在这个问题上也挣扎了一段时间,并求助于使用配件创建自定义图像.但我刚刚发现这个解决方案效果很好并且不需要自定义图像.诀窍是更改单元格的 backgroundView 颜色而不是 backgroundColor.

I struggled with this one for a little while too and resorted to creating a custom image with the accessory. But I just found this solution that works well and doesn't require a custom image. The trick is to change the cell's backgroundView color not the backgroundColor.

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

无需更改附件视图或内容视图的背景颜色.他们会自动跟进.

No need to change the accessoryView or contentView background colors. They'll follow automatically.

2014 年的注意事项.通常你会使用 -(void)setSelected:(BOOL)selected animation:(BOOL)animated

Note for 2014. Very typically you wold use -(void)setSelected:(BOOL)selected animated:(BOOL)animated

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

So, you'd have a custom cell class, and you'd set the colours for the normal/selected like this...

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

希望对某人有所帮助.

这篇关于设置 UITableViewCell 的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:25