0

我正在使用UITableView. 在我的表格视图中,我有多个包含多个单元格的部分。我想为每个单元格赋予单元格背景颜色。这意味着each cell should have different colors

例如:如果有 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

我想像这样给表格视图单元格背景颜色。然后当 UITableView 重新加载或新打开时,背景颜色会发生变化。你能帮我这样做吗?可以在 iPhone app tableview 中做吗?

提前致谢。

4

3 回答 3

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 */;
}
于 2012-05-07T13:39:52.883 回答
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 的背景颜色。

干杯!

于 2012-05-07T16:22:10.130 回答
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;
}
于 2012-05-07T14:04:22.957 回答