2012-05-07 86 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