UITableViewCell清除背景-分组UITableView

4
我将尝试按以下方式展示我的数据:
如下图所示,我已通过在viewDidLoad中编写以下代码来清除表格的背景视图:

enter image description here

customerTable.backgroundView = nil;  

在我的xib文件中,我已经清除了表格的背景。

cellForRowAtIndexPath方法中,我为每个表格单元格设置了背景颜色。

cell.backgroundColor = [UIColor colorWithRed:255.0/255 green:235.0/255 blue:178.0/255 alpha:1];  

作为结果,我得到了上面的输出。
我面临的问题是每个单元格中出现的黑色角落。我知道这与单元格的背景有关,但对我来说没有任何作用。我还尝试清除单元格背景,写入以下代码行:
    cell.backgroundView = nil;  

你正在创建自己的UITableViewCell子类吗? - Carter
2
如果您将cell.backgroundColor设置为[UIColor clearColor],会发生什么?黑色的角落还在吗? - Carter
现在没有拐角。但是我该如何获取单元格的背景颜色,以便设置并且仍然不显示拐角呢? - Nitish
2
你可以尝试重写单元格的绘制方式,或者在单元格中添加另一个带有圆角的子视图(同时保持背景颜色透明)。你是如何为UITableViewCell获取圆角的? - Carter
我之前考虑过你提到的选项。我在想,也许我可以在带有圆角的单元格上放置一张图片,然后将单元格的背景颜色设置为透明色。如果可能的话,我只是想不使用图片来完成它。谈到我如何在单元格中获得圆角,我认为当我们清除单元格的背景视图时,就会留下一个圆角的单元格。在文本字段的情况下也是如此。 - Nitish
1
我不确定为什么会出现黑色的角落,但是你可以通过子类化UITableViewCell并绘制一个圆角矩形来代替基本的绘图。这样做可以让你控制角落的圆润程度和整个单元格的颜色,同时消除黑色角落的问题。 - Carter
6个回答

8

请尝试:

customerTable.backgroundColor = [UIColor clearColor];
customerTable.opaque = NO;
customerTable.backgroundView = nil;

这段代码涉及到的是设置表格的背景颜色和透明度,以及清除表格的背景视图。

我已经提到过,在我的xib中,我已经清除了表格的背景。 - Nitish
opaque 设置为 NO 是关键,我认为。 - mrueg
大家好。在发布这个问题之前,我已经尝试了Perrie建议的所有方法,但都没有用。 - Nitish
@Jolly:如果我想清除的是表格背景,他是正确的。但是我已经明确提到了我担心的是部分内部的单元格。 - Nitish

0

我曾经遇到过同样的问题。后来发现这与单元格的背景无关。

问题出在表格视图的背景上,导致了这些奇怪的角落,所以

self.tableview.backgroundColor = [UIColor clearColor];

viewWillAppear中解决问题 :)

0

尝试这段代码......

customerTable.separatorColor = [UIColor clearColor];

0

尝试

- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath

{

  cell.backgroundColor = [UIColor clearColor];

}

这将清除单元格的背景。我希望颜色仍然存在,但没有黑色角落。 - Nitish

0

试一下吧:

将单元格背景视图设置为YourcellImage,单元格背景颜色设置为透明色。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath             
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

        [cell setBackgroundColor:[UIColor clearColor]];
        UIImage *backgroundImage = nil;
        UIImageView *backgroundView = nil;
        backgroundImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"YourYellowCellImage"ofType:@"png"]];
        backgroundView = [[UIImageView alloc]initWithImage:backgroundImage];
        backgroundView.frame = CGRectMake(0, 0, 600, 80); // Over here give your cell size
        cell.backgroundColor = [UIColor clearColor];
        cell.backgroundView =backgroundView;

        [backgroundView release];   
   }
   //Over your set your Label Value    
   return cell;
}

0
在cellForRowAtIndex方法中清除背景应该可以解决问题。
[cell setBackgroundColor:[UIColor clearColor]];

尝试在viewWillAppear中清除背景,如果发生了奇怪的事情,请清除项目并重新构建。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接