【问题标题】:Changing background color of a custom UITableViewCell cell in a plain styled UITableView在普通样式的 UITableView 中更改自定义 UITableViewCell 单元格的背景颜色
【发布时间】:2011-07-08 08:19:40
【问题描述】:

我正在尝试以普通样式 UITableView 更改自定义 UITableViewCell 的背景颜色。

我已经阅读了其他类似的问题,并且我在我的委托方法中:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath

我设置了cell.backgroundColor = [UIColor...]。问题是这只适用于分组样式UITableView

注意:我想以编程方式而不是在我的 .xib 文件中执行此操作的原因是因为我希望不同的单元格有不同的背景颜色。

【问题讨论】:

  • 尝试更改内容视图的背景颜色。 cell.contentView.backGroundColor=[UIColor ...];

标签: iphone objective-c cocoa-touch uitableview


【解决方案1】:

也许您可以在设置颜色之前添加cell.contentView.backgroundColor = [UIColor clearColor]

原因是背景视图在底部。内容视图位于顶部。

【讨论】:

  • 成功了,谢谢。知道 cell.backgroundColor 和 cell.contentView.backgroundColor 之间有什么区别吗?
  • 该单元格有两层。一层是背景视图,另一层是内容视图。背景视图上有苹果提供的一些按钮。如果您尝试 tableView.editing = YES,您可以看到删除按钮。它在背景视图上
  • 抱歉,仔细一看,这不起作用。我确实将 cell.contentView.backgroundColor 设置为我直接想要的颜色,并且确实有效。所以请改变你的答案。谢谢
  • 啊,当你声明你的 tableview 时尝试设置 yourtableview.editing = YES。看看会发生什么
【解决方案2】:

在以下委托方法中更改颜色:

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

【讨论】:

    【解决方案3】:
    // set selection color
    UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
    
    myBackView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.75 alpha:1];
    
    [cell.contentView addSubview:myBackView];
    
    [myBackView release];
    

    像这样根据需要更改每个单元格

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-15
      • 1970-01-01
      • 2011-09-12
      相关资源
      最近更新 更多