2012-05-07 85 views
0

我正在爲使用UITableView的iPhone應用程序工作。在我的tableview中,我有多個單元格的多個部分。我想給每個單元格的單元格背景顏色。這意味着each cell should have different colors。例如:如果有20個單元,則每個20個單元應該具有不同的單元背景顏色。在iPhone應用程序中更改每個UITableViewCell背景顏色?

你能告訴我是否可以給每個細胞提供細胞背景顏色/圖像嗎? 你能指導我做這個嗎?

在此先感謝。

編輯

我Tableviewcell顏色就會像下面,

First Cell background color : white color 
Second Cell background color : red color 
Third Cell background color : orange color 
Fourth cell background color : gray color 
Fifth Cell background color : white color 
Sixth cell background color : white color 
Seventh cell background color : white color 
Eight cell background color : gray color 
Ninth cell background color : red color 
Tenth cell background color : red color 

我想給的tableview單元格背景顏色也贊。然後當UITableView重新加載或重新打開時,背景顏色會發生變化。你能幫我這樣做嗎? iPhone應用程序tableview中可以做什麼?

在此先感謝。

+0

可能的重複http://stackoverflow.com/questions/281515/how-to-customize-the-background-color-of-a-uitableviewcell – 2012-05-07 13:25:54

回答

0

實際上,您無法使用標準tableView:cellForRowAtIndexPath:工作流程來執行此操作。單元格的背景顏色是一種特殊情況,因爲表格視圖將在管理單元格選定狀態的同時對其進行修改。

相反,如this answer所述,您必須實施tableView:willDisplayCell:atIndexPath:方法。有一箇舊的WWDC視頻涵蓋了這一點,我認爲從2009年或2010年的會議。

例如,你可以做這樣的事情:

- (void)tableView:(UITableView*)tableView 
    willDisplayCell:(UITableViewCell*)cell 
forRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
    cell.backgroundColor = /* random color here */; 
} 
+0

其實,你可以在'cellForRowAtIndexPath'中做到,它只是一種複雜的。我將發佈一個答案,展示如何。 – AMayes

+0

對不起,但這是蘋果工程師至少在一個WWDC視頻中涉及的非常着名的限制。使用cellForRowAtIndexPath不可靠,你會得到奇怪的行爲和不一致的結果。唯一可靠且官方支持的配置單元背景顏色的方法是使用willDisplayCell。 –

0

試試這個

- (UITableViewCell *)tableView:(UITableView *)tTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    double numSections = [self numberOfSectionsInTableView:tableView]; 
    double numRows = [self tableView:tableView numberOfRowsInSection:indexPath.section]; 
    [cell setBackgroundColor:[UIColor colorWithRed:0.8/numRows*((double)indexPath.row)+0.2 green:0.8/numSections*((double)indexPath.section)+0.2 blue:((double)(random()%1000))/1000.0 alpha:1]]; 
    return cell; 
} 
0

這是一個有點複雜,但這裏是如何做到這一點的的cellForRowAtIndexPath。只需將此代碼粘貼到您的方法中。我根據你的規格寫了它。

if (indexPath.row == 0 || indexPath.row == 4 || indexPath.row == 5 || indexPath.row == 6) { 
      [cell.textLabel setBackgroundColor:[UIColor whiteColor]]; 
      cell.contentView.backgroundColor = [UIColor whiteColor]; 
      [cell.textLabel setTextColor:[UIColor blackColor]]; 
     } else { 
      if (indexPath.row == 1 || indexPath.row == 8 || indexPath.row == 9){ 
       [cell.textLabel setBackgroundColor:[UIColor redColor]]; 
       cell.contentView.backgroundColor = [UIColor redColor]; 
       [cell.textLabel setTextColor:[UIColor whiteColor]]; 
      } else { 
        if (indexPath.row == 3 || indexPath.row == 7){ 
       [cell.textLabel setBackgroundColor:[UIColor grayColor]]; 
       cell.contentView.backgroundColor = [UIColor grayColor]; 
       [cell.textLabel setTextColor:[UIColor whiteColor]]; 
       } else { 
         [cell.textLabel setBackgroundColor:[UIColor orangeColor]]; 
         cell.contentView.backgroundColor = [UIColor orangeColor]; 
         [cell.textLabel setTextColor:[UIColor blackColor]]; 
}}} 

有,當然,其他的方法來做到這一點(例如,使用switch,或通過具有聲明的UIColor,並改變根據其indexPath.row被稱爲它的值)。但實施後,你可以自己簡化它。

請注意,如果需要均勻性,則需要設置textLabel和contentView的背景顏色。

乾杯!

+1

除非你想隨機着色。然後,這完全是一個錯誤的方式去關於它大聲笑。 – AMayes