如何更改uitableViewCell背景颜色

时间:2011-12-02 07:07:48

标签: iphone uitableview

注意:这是关于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时纹理背景保留在相同位置。

enter image description here

2 个答案:

答案 0 :(得分:0)

将tableView Background设置为这样的清晰颜色,

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

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

答案 1 :(得分:0)

我不知道你为什么要将子视图添加到tableview,你可以为tableview设置背景颜色:

tview.backgroundColor=[UIColor yellowColor];