2011-09-21 51 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