代码之家  ›  专栏  ›  技术社区  ›  rpj

更改UITableViewCell中UILabel的背景色

  •  11
  • rpj  · 技术社区  · 16 年前

    UITableViewCell是“预构建”的,其中UILabel是初始化后唯一的子视图。我愿意

    UILabel* label = (UILabel*)[cell.contentView.subviews objectAtIndex:0];
    label.textColor = [UIColor whiteColor];
    label.backgroundColor = [UIColor darkGrayColor];
    label.opaque = YES;
    
    4 回复  |  直到 7 年前
        1
  •  8
  •   Hemang    12 年前

    您的代码片段对我来说很好,但我相信它必须在单元格被添加到表中并显示之后完成。如果从 initWithFrame:reuseIdentifier: UILabel 子视图 尚未创建。

    也许最好的解决办法是添加您自己的 UILabel ,配置为您的标准,而不是依赖于此(非常不稳定)的内置路径。

        2
  •  5
  •   TomSwift    14 年前

    这不起作用,因为UITableViewCell在layoutSubviews方法中设置其标签背景色。

    - (void) layoutSubviews
    {   
         [super layoutSubviews];
    
         self.textLabel.backgroundColor = [UIColor redColor];
    }
    
        3
  •  2
  •   Kendall Helmstetter Gelner    16 年前

    在分配单元格时,将自己的标签添加到contentView,而不是依赖于提取内置标签。然后可以控制所有值:

    UILabel* label = [[[UILabel alloc] init] autorelease];
    label.textColor = [UIColor whiteColor];
    label.backgroundColor = [UIColor darkGrayColor];
    label.opaque = YES;
    [cell.contentView addSubview:label];
    
        4
  •  0
  •   Adi edson    12 年前
    for (UIView *views in views.subviews)
    {
        UILabel* temp = (UILabel*)[views.subviews objectAtIndex:0];
        temp.textColor = [UIColor whiteColor];        
        temp.shadowColor = [UIColor blackColor];
        temp.shadowOffset = CGSizeMake(0.0f, -1.0f);
    }