开发者

Cell background and accessory

I am trying to set the cell background of my UITableViewCells but I am struggling with the background color of the UIAccessoryView. It does not change.

Can anyone help me make the background color of the UIAccessoryView transparent (or开发者_StackOverflow actually any other color)?

This is my code

cell.textLabel.backgroundColor = [UIColor clearColor];
cell.accessoryView.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

if(indexPath.row % 2) {
     cell.contentView.backgroundColor = [UIColor clearColor];
}
else {
    cell.contentView.backgroundColor = [UIColor redColor];
}

Here is an image illustrating the issue

Cell background and accessory


You'll need to set the backgroundView property of the UITableViewCell. For example:

UIView* myBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
myBackgroundView.backgroundColor = [UIColor redColor];
myCell.backgroundView = myBackgroundView;

If you want something more fancy, like a red gradient background, you would implement a subclass of UIView that draws a gradient in drawRect:, and use it as the backgroundView.

You can also set the selectedBackgroundView property if you want a custom look for a selected table cell.


In function

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

Use following code

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

reference for more details


If you want to change it in custom class of UITableViewCell you can just use:

override func layoutSubviews() {
    super.layoutSubviews()
    self.backgroundColor = UIColor.whiteColor()
}

changing background inside of awakeFromNib() or init?(coder aDecoder: NSCoder) doesn't work!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜