2011-09-21 21 views
0

我更改了我的UITableView(带有图案图像)的backgroundColor,并且我希望我的单元格完全变白,我将backgroundColor指定为cell.contentView。我无法更改accessoryTypebackgroundColor。我怎样才能做到这一点 ?无法更改tableView中的indicatorView.backgroundColor查看

这里是我有什么暂时的说明:

enter image description here

- (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.backgroundColor = [UIColor whiteColor]; 
    } 

    // Configure the cell... 

    return cell; 
} 

回答

0

设置表格视图单元格的背景颜色,而不是内容视图的颜色。

cell.backgroundColor = [UIColor whiteColor]; 

内容视图仅用于表格视图单元格管理的内容。任何其他视图(如表格处于编辑模式时的访问器视图或重新排序视图)均由表视图和表视图单元类内部处理。他们根据需要调整内容视图的大小,因此您不能依赖内容视图来设置背景颜色。

编辑:对不起,这是不正确。在表视图单元格上设置基于状态属性的正确位置在方法UITableViewDelegate中。在这里设置单元格的背景颜色。

+0

如果我将我的颜色分配给contentView,它不起作用。这是因为它不会改变任何事情。显示的颜色是tableView.backgroundColor ... – klefevre

+0

对不起,我错了。看我的编辑。 – LucasTizma

+0

omg我忘了这个委托方法,我无法理解为什么Apple强制在这个方法中分配一个单元格backgroundColor,而不是在tableView:cellForRowAtIndexPath:无论如何 – klefevre

-1

我不认为你可以更改标准附件视图的背景颜色。而不是使用UITableViewCellAccessoryDisclosureIndicator,使用您的自定义图像将cell.accessoryView设置为UIImageView。

+0

没有理由去这个麻烦时,正确的做法是设置单元格的'backgroundColor'属性,而不是单元格的内容视图的'backgroundColor'财产。 – LucasTizma