【问题标题】:Objective C: How to add color to entire cell (with a disclosure indicator)目标 C:如何为整个单元格添加颜色(带有披露指示符)
【发布时间】:2011-10-24 06:37:46
【问题描述】:

我正在尝试为我的单元格添加背景颜色,但无法使用披露指示器为该部分着色(请参见下面的屏幕截图)

如何为整个单元格着色?我的实现如下。

cell.contentView.backgroundColor = [UIColor colorWithHexString:@"#FFFFCC"];

//Clear colors for labels
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];

【问题讨论】:

    标签: objective-c ios uitableview background-color


    【解决方案1】:

    UITableViewCell 子类 UIView;您可以像设置任何其他视图一样设置其背景颜色。 “分组”样式的表格视图中的单元格需要更多的工作,但看起来你并没有使用它,所以:

    cell.backgroundColor = [UIColor colorWithHexString:@"#FFFFCC"];
    

    【讨论】:

    • 谢谢!但是,它不起作用,因为颜色现在甚至没有出现在单元格中
    • 有趣。这曾经奏效。在这种情况下,您需要使用适当的框架 (0, 0, cell.bounds.size.width, cell.bounds.size.height) 创建一个额外的 UIView 并设置其 backgroundColor,然后将该视图设置为单元格的 backgroundView
    • 但这不会掩盖我的披露指标吗?
    • 没有。 backgroundView 放置在单元格的背景中,位于内容视图和任何附件视图(如披露指示器)下方。
    • cell.background color =[UIColor colorWithHexString:@"#FFFFCC"] 如果我将代码放入方法中 - (void)tableView: (UITableView*)tableView willDisplayCell:(UITableViewCell*)单元格 forRowAtIndexPath:(NSIndexPath*)indexPath。它也涵盖了披露背后的领域。
    【解决方案2】:

    我的代码使用(在单元格本身中)self.backgroundColor,因此您应该使用 cell.backgroundColor 而不是 cell.contentView.backgroundColor。 contentView 不包括选择指示器视图,我猜你猜到了;)

    如果您只希望它在选中后更改颜色,请使用 selectedBackgroundView.backgroundColor

    【讨论】:

    • @Khelder 谢谢!但颜色根本没有出现在我的单元格中。
    • 你也可以设置tableview本身的颜色,用tableview.backgroundColor,如果它更适合你的话。
    【解决方案3】:

    UIImageView

    UIImageView 右侧带有人字形图像。最好在情节提要中,但可以通过将其附加到内容视图来轻松地在代码中进行管理。

    【讨论】:

      【解决方案4】:

      使用与单元格相同的边界为 uiview 着色并将其添加为子视图

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } UIView *colorView = [cell viewWithTag:200]; if(!colorView){ colorView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 44)] autorelease]; colorView.backgroundColor = [UIColor redColor]; colorView.tag = 200; [cell addSubview:colorView]; } return cell; }

      然后将所有子视图添加到该彩色视图。

      【讨论】:

        【解决方案5】:

        接受的答案对我不起作用,但以下代码有效:

        [cell.contentView setBackgroundColor:[UIColor whiteColor]];
        

        希望这对某人有所帮助

        【讨论】:

          【解决方案6】:

          这让我发疯了。我在 cellForRowAtIndexPath 方法中尝试了许多不同的方法。我终于找到了这样做的方法是像这样覆盖 willDisplayCell 方法:

          - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
          {
              if(indexPath.row % 2) {
                  cell.backgroundColor = [UIColor whiteColor];
              } else {
                  cell.backgroundColor = [UIColor colorWithRed:225.0/255.0 green:225.0/255.0 blue:225.0/255.0 alpha:1.0];
              }
          }
          

          【讨论】:

            【解决方案7】:

            因为它是一个UIView 子类,你应该可以设置附件视图的背景颜色:

            cell.accessoryView.backgroundColor = [UIColor clearColor];
            

            或:

            cell.accessoryView.backgroundColor = [UIColor colorWithHexString:@"#FFFFCC"];
            

            【讨论】:

            • 奇怪的是,这没有任何效果,我仍然看到我的披露背后的白色背景 (cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator)
            • 附件视图供您设置。仅设置附件类型时不会设置。因此,预期的行为是这些分配没有效果(如您所见)。
            猜你喜欢
            • 2012-07-09
            • 2010-11-19
            • 1970-01-01
            • 2018-10-20
            • 1970-01-01
            • 1970-01-01
            • 2020-03-23
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多