【问题标题】:How to customize the background color of a UITableViewCell?如何自定义 UITableViewCell 的背景颜色?
【发布时间】:2010-09-21 20:15:34
【问题描述】:

我想自定义 UITableView 中所有 UITableViewCells 的背景(也可能还有边框)。到目前为止,我还不能自定义这些东西,所以我有一堆默认的白色背景单元格。

有没有办法用 iPhone SDK 做到这一点?

【问题讨论】:

    标签: ios iphone uitableview cocoa-touch background-color


    【解决方案1】:

    这是我遇到的解决此问题的最有效方法,使用 willDisplayCell 委托方法(这在使用 cell.textLabel.text 和/或单元格时也会处理文本标签背景的白色。 detailTextLabel.text):

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { ... }
    

    当调用此委托方法时,单元格的颜色是通过单元格而不是表格视图控制的,就像您使用时一样:

    - (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { ... }
    

    因此,在单元格委托方法的主体中,添加以下代码以交替使用单元格颜色,或者仅使用函数调用使表格的所有单元格具有相同的颜色。

    if (indexPath.row % 2)
    {
        [cell setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];
    }
    else [cell setBackgroundColor:[UIColor clearColor]];
    

    这个解决方案在我的情况下效果很好......

    【讨论】:

    • 这是一种更清洁的解决方案。大多数其他解决方案都需要子类化 UITableViewCell。 vlado.grigorov 的解决方案对我不起作用。
    • 确实,这是苹果自己根据我看过的各种 WWDC 演示推荐的方法。
    • 很好 - 除非您需要在列表顶部添加新行。此方法使所有添加的颜色相同。刷新确实可以修复它,但会花费不必要的时间。昨天发现...
    • 这似乎运作良好,除非使用 Core Data。通过 NSFetchedResultsController 添加的新单元格似乎无法正确调用 willDisplayCell。我不知道让它工作......
    • 在 Swift 2.0 中设置背景颜色的等效行:cell.backgroundColor = UIColor.clearColor
    【解决方案2】:

    您需要将单元格的 contentView 的 backgroundColor 设置为您的颜色。如果您使用配件(例如披露箭头等),它们将显示为白色,因此您可能需要滚动这些配件的自定义版本。

    【讨论】:

    • 例如myCell.contentView.backgroundColor = [ UIColor greenColor ];
    • vlado.grigorov 的回答更灵活 - 请参阅 William Denniss 的回答
    • 例如,关于如何滚动自己的UITableViewCellAccessoryCheckmark 的任何链接?谢谢。
    • 有时需要设置才能看到:cell.textLabel.backgroundColor = [UIColor clearColor];
    【解决方案3】:

    这真的很简单,因为 OS 3.0 只是在 willDisplayCell 方法中设置单元格的背景颜色。 不得在 cellForRowAtIndexPath 中设置颜色。

    这适用于普通样式和分组样式:

    代码:

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        cell.backgroundColor = [UIColor redColor];
    }
    

    P.S:这里是 willDisplayCell 的文档摘录:

    "表格视图将此消息发送到 它的代表就在它使用单元之前 画一排,从而允许 委托自定义单元格对象 在显示之前。这种方法 让代表有机会 覆盖基于状态的属性集 更早的由表视图,如 选择和背景颜色。后 委托返回,表格视图 仅设置 alpha 和 frame 属性,然后仅当 当它们滑入时动画行或 出去。”

    我在this post from colionel 中找到了此信息。谢谢他!

    【讨论】:

    • 这应该被标记为正确答案,因为如果你有一个公开Indiator,你甚至不需要做任何事情
    • 如果你想为我的表格视图背景使用不同的单元格背景颜色,这不起作用。
    • 对于group cell,可以在cellForRow上设置
    【解决方案4】:

    到目前为止,我发现的最佳方法是设置单元格的背景视图并清除单元格子视图的背景。当然,这在只有索引样式的表格上看起来不错,无论是否有配件。

    这是一个单元格背景为黄色的示例:

    UIView *backgroundView = [ [ [ UIView alloc ] initWithFrame:CGRectZero ] autorelease ];
    backgroundView.backgroundColor = [ UIColor yellowColor ];
    cell.backgroundView = backgroundView;
    for ( UIView *view in cell.contentView.subviews ) 
    {
        view.backgroundColor = [ UIColor clearColor ];
    }
    

    【讨论】:

    • 如果这样做,请确保将 opaque 属性也设置为 NO。
    • 真的不推荐使用透明单元格控件。滚动性能会受到很大影响,这就是为什么 Apple 总是说要使用不透明控件。
    • 在 iOS 4.2 及更高版本上,似乎不再需要循环。
    • 非常好。使用附件视图时完美!
    【解决方案5】:

    只需将它放在你的 UITableView 委托类文件 (.m) 中:

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell  forRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        UIColor *color = ((indexPath.row % 2) == 0) ? [UIColor colorWithRed:255.0/255 green:255.0/255 blue:145.0/255 alpha:1] : [UIColor clearColor];
        cell.backgroundColor = color;
    }
    

    【讨论】:

      【解决方案6】:

      我同意 Seba, 我尝试在 rowForIndexPath 委托方法中设置交替行颜色,但在 3.2 和 4.2 之间得到不一致的结果。以下内容对我很有用。

      - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
      
          if ((indexPath.row % 2) == 1) {
              cell.backgroundColor = UIColorFromRGB(0xEDEDED);
              cell.textLabel.backgroundColor = UIColorFromRGB(0xEDEDED);
              cell.selectionStyle = UITableViewCellSelectionStyleGray;
          }
          else
          {
              cell.backgroundColor = [UIColor whiteColor];
              cell.selectionStyle = UITableViewCellSelectionStyleGray;
          }
      
      }
      

      【讨论】:

      • 仍然不明白为什么在 willDisplayCell 中设置它会有什么不同。不管我们实现与否,它都会被调用?
      【解决方案7】:

      在尝试了所有不同的解决方案之后,以下方法是最优雅的方法。 在以下委托方法中更改颜色:

      - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
          if (...){
              cell.backgroundColor = [UIColor blueColor];
          } else {
              cell.backgroundColor = [UIColor whiteColor];
          }
      }
      

      【讨论】:

        【解决方案8】:

        vlado.grigorov 有一些很好的建议 - 最好的方法是创建一个 backgroundView,并给它一个颜色,将其他所有内容设置为 clearColor。此外,我认为这种方式是正确清除颜色的唯一方法(尝试他的示例 - 但设置 'clearColor' 而不是 'yellowColor'),这就是我想要做的。

        【讨论】:

        • 这种方法的一个优点是您可以将颜色保留为 Interface Builder 中的设计时属性。需要开发人员互动的设计问题少了一个。
        • cell.backgroundView = [[[UIView alloc] initWithFrame: cell.bounds ] autorelease]; cell.backgroundView.backgroundColor = [UIColor redColor];
        【解决方案9】:

        自定义表格视图单元格的背景最终成为“全有或全无”的方法。很难以一种看起来不奇怪的方式更改用于内容单元格背景的颜色或图像。

        原因是单元格实际上跨越了视图的宽度。圆角只是其绘图风格的一部分,内容视图位于此区域。

        如果您更改内容单元格的颜色,您最终会在角落看到白色位。如果您更改整个单元格的颜色,您将拥有一个横跨视图宽度的颜色块。

        您当然可以自定义一个单元格,但它并不像您最初想象的那么简单。

        【讨论】:

        • 这对于分组表中的单元格绝对正确,但普通表不必担心太多。没有配件的朴素细胞应该看起来不错。
        • 本 - 好点。我主要使用分组表,但正如您提到的普通表不会遇到任何这些问题。
        【解决方案10】:

        创建一个视图并将其设置为单元格的背景视图

        UIView *lab = [[UIView alloc] initWithFrame:cell.frame];
                    [lab setBackgroundColor:[UIColor grayColor]];
                    cell.backgroundView = lab;
                    [lab release];
        

        【讨论】:

          【解决方案11】:

          扩展 N.Berendt 的回答 - 如果您想根据实际单元格数据对象中的某些状态设置单元格颜色,则在您配置表格单元格的其余信息时,通常是当您在 cellForRowAtIndexPath 方法中,您可以通过覆盖 willDisplayCell 方法以从内容视图背景颜色设置单元格背景颜色来做到这一点 - 这解决了披露按钮背景等问题不正确但仍然允许您控制颜色cellForRowAtIndexPath 方法,您在其中进行所有其他单元格自定义。

          所以: 如果将此方法添加到表视图委托中:

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

          然后在你的 cellForRowAtIndexPath 方法中你可以这样做:

          if (myCellDataObject.hasSomeStateThatMeansItShouldShowAsBlue) {
              cell.contentView.backgroundColor = [UIColor blueColor];
          }
          

          这省去了在 willDisplayCell 方法中再次检索数据对象的麻烦,还省去了在两个地方对表格单元格进行剪裁/自定义 - 所有自定义都可以在 cellForRowAtIndexPath 方法中进行。

          【讨论】:

            【解决方案12】:

            使用 Photoshop 或 gimp 创建一个图像以用作背景,并将其命名为 myimage。 然后,将此方法添加到您的 tableViewController 类中:

            - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
                UIImage *cellImage = [UIImage imageNamed:@"myimage.png"];//myimage is a 20x50 px with  gradient  color created with gimp
                UIImageView *cellView = [[UIImageView alloc] initWithImage:cellImage];
                cellView.contentMode = UIContentViewModeScaleToFill;
                cell.backgroundView = cellView;
                //set the background label to clear
                cell.titleLabel.backgroundColor= [UIColor clearColor];
            }
            

            如果您在属性检查器中将 UITableView 设置为自定义,这也将起作用。

            【讨论】:

              【解决方案13】:

              我的解决方案是在 cellForRowAtIndexPath 事件中添加以下代码:

              UIView *solidColor = [cell viewWithTag:100];
              if (!solidColor) {
              
                  solidColor = [[UIView alloc] initWithFrame:cell.bounds];
                  solidColor.tag = 100; //Big tag to access the view afterwards
                  [cell addSubview:solidColor];
                  [cell sendSubviewToBack:solidColor];
                  [solidColor release];
              }
              solidColor.backgroundColor = [UIColor colorWithRed:254.0/255.0
                                                           green:233.0/255.0
                                                            blue:233.0/255.0
                                                           alpha:1.0];
              

              在任何情况下都可以工作,即使使用披露按钮也是如此,并且我认为在 cellForRowAtIndexPath 中对单元格颜色状态进行操作比在 cellWillShow 事件中更适合您的逻辑。

              【讨论】:

                【解决方案14】:

                继承 UITableViewCell 并将其添加到实现中:

                -(void)layoutSubviews
                {
                    [super layoutSubviews];
                    self.backgroundColor = [UIColor blueColor];
                }
                

                【讨论】:

                  【解决方案15】:
                      UIView *bg = [[UIView alloc] initWithFrame:cell.frame];
                      bg.backgroundColor = [UIColor colorWithRed:175.0/255.0 green:220.0/255.0 blue:186.0/255.0 alpha:1]; 
                      cell.backgroundView = bg;
                      [bg release];
                  

                  【讨论】:

                    【解决方案16】:

                    这将适用于最新的 Xcode。

                    -(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath {
                        cell.backgroundColor = [UIColor grayColor];
                    }
                    

                    【讨论】:

                      【解决方案17】:

                      试试这个:

                      - (void)tableView:(UITableView *)tableView1 willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
                      {
                          [cell setBackgroundColor:[UIColor clearColor]];
                          tableView1.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"Cream.jpg"]];
                      }
                      

                      【讨论】:

                        猜你喜欢
                        • 2017-07-06
                        • 1970-01-01
                        • 2015-03-15
                        • 2012-06-05
                        • 1970-01-01
                        • 1970-01-01
                        • 2011-02-03
                        • 1970-01-01
                        • 2020-12-14
                        相关资源
                        最近更新 更多