【问题标题】:How to change tableViewCell background color如何更改 tableViewCell 背景颜色
【发布时间】:2011-12-02 07:07:48
【问题描述】:

注意:这是关于GROUPED tableView 中的一个单元格。与普通的 tableView 相比,这有很大的不同!默认单元格自定义答案在这种情况下不起作用,因此请先验证您的答案。

这是我设置灰屏黄色tableView背景的方式:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    UIView *myView = [[UIView alloc] initWithFrame:magicRect];
    myView.backgroundColor = [UIColor yellowColor];
    [self.tableView addSubview:myView];
    [self.tableView sendSubviewToBack:myView];
}

这就是我设置绿色单元格背景的方式。从图片中可以看出,它缺少一些区域:

- (UITableViewCell *)tableView:(UITableView *)tableView
   cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 UITableViewCell *cell =
   [tableView dequeueReusableCellWithIdentifier:@"Cell"];
 if (cell == nil) {
   cell = [[[UITableViewCell alloc]
     initWithStyle:UITableViewCellStyleValue1
     reuseIdentifier:@"Cell"] autorelease];
   cell.backgroundColor = [UIColor greenColor];
 }
 // Configure the cell...
}

问题:我如何在 tableView 单元格的开头和结尾更改颜色?现在单元格在这些区域中是透明的,并从整个 tableView 下方显示 self.view.backgroundColor。这些区域确实是透明的,因为在滚动 tableView 时,纹理背景保持在同一位置。

【问题讨论】:

  • 嗨,请不要批准such a too minor suggested edit。将在您回复后删除评论:)
  • 任何提高可读性的编辑都是我很乐意批准的。检查了情况,将再次批准。很抱歉,可读性和简单性对我来说很重要......顺便说一句,这个问题有什么提示吗? :)

标签: iphone uitableview


【解决方案1】:

将tableView背景设置为像这样的清晰颜色,

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

     [tableView setBackgroundColor:[UIColor clearColor]];//Here you can give as yellow instead of adding view
     //ur code
}

【讨论】:

  • 抱歉,我也想保留灰色背景色。这个想法是为 tableView 区域和 tableView 之外的区域设置不同的颜色。此代码会覆盖灰色背景。
  • 你保持 self.view.backgroundColor = [UIColor grayColor];照原样将tableView的背景设为黄色
  • 使用分组的tableView时,tableView的backgroundColor会覆盖view的backgroundColor。
【解决方案2】:

不知道为什么要给tableview加subview,可以给tableview设置背景色:

tview.backgroundColor=[UIColor yellowColor];

【讨论】:

  • 因为我想要 tableView 和 tableView 之外的区域有不同的颜色。这是一个 GROUPED tableView,其行为方式与普通 tableView 不同。
猜你喜欢
  • 1970-01-01
  • 2011-11-11
  • 1970-01-01
  • 1970-01-01
  • 2016-04-08
  • 2018-02-13
  • 2012-01-02
  • 2022-01-21
  • 2016-05-31
相关资源
最近更新 更多