在iPhone应用程序中更改每个UITableViewCell背景颜色?

时间:2012-05-07 13:23:10

标签: iphone ios uitableview background-color

使用UITableView为iPhone应用程序工作。在我的tableview中,我有多个包含多个单元格的部分。我想给每个细胞提供细胞背景颜色。这意味着each cell should have different colors

Ex:如果有20个细胞意味着每个20个细胞应该具有不同的细胞背景颜色。

请告诉我是否可以为每个细胞提供细胞背景颜色/图像? 你能指导我这样做吗?

提前致谢。

修改

我的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 app tableview中做到吗?

提前致谢。

3 个答案:

答案 0 :(得分:0)

您实际上无法使用标准tableView:cellForRowAtIndexPath:工作流来执行此操作。单元格的背景颜色是一种特殊情况,因为表格视图将在管理单元格的选定状态时对其进行修改。

相反,正如this answer中所述,您必须实施tableView:willDisplayCell:atIndexPath:方法。从2009年或2010年的会议开始,有一个旧的WWDC视频可以覆盖这个视频。

例如,您可以执行以下操作:

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

答案 1 :(得分: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;
}

答案 2 :(得分: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的背景颜色。

干杯!