开发者

Cannot change indicatorView.backgroundColor in a tableView

开发者 https://www.devze.com 2023-04-06 06:18 出处:网络
I changed the backgroundColor of my UITableView (with a pattern image) and I want my cells entirely white, I assigned a backgroundColor to cell.contentView. I cannot change the backgroundColor of acce

I changed the backgroundColor of my UITableView (with a pattern image) and I want my cells entirely white, I assigned a backgroundColor to cell.contentView. I cannot change the backgroundColor of accessoryType. How can I do that ?

Here is an illustration of what I have for the moment:

Cannot change indicatorView.backgroundColor in a tableView

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *bgImg = [UIImage imageNamed:@"backgroundPattern.png"];
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:bgImg];
}

- (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];
        // Put an arrow
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        // In order to have my cell with a white background. I assign a whiteColor
        // to my contentView because assign color to the cell.backgroundColor property doesn't
        // to work..
        cell.contentView.backgroundColor = [UIColor whiteColor];

        // HERE is my problem, I would assign a backgroundColor of my accessoryType.
        // Because for the moment, just behind the accessoryType this is a the color of the
        // tableView.backgroundColor
        cell.accessoryView.backgrou开发者_StackOverflow社区ndColor = [UIColor whiteColor];
    }

    // Configure the cell...

    return cell;
}


Set the table view cell's background color, not the content view's.

cell.backgroundColor = [UIColor whiteColor];

The content view is only for the content your table view cell manages. Any additional views (like the accessor view or the reordering views when the table is in edit mode) are handled internally by the table view and table view cell class. They resize the content view as necessary, so you can't rely on the content view to set the background color.

EDIT: Sorry, this is incorrect. The correct place to set state-based properties on the table view cell is in the -tableView:willDisplayCell:forRowAtIndexPath: method of UITableViewDelegate. Set the cell's background color here.


I don't think you can change the background color of a standard accessory view. Instead of using UITableViewCellAccessoryDisclosureIndicator, set cell.accessoryView to an UIImageView, using your custom image.

0

精彩评论

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