UITableView清晰的背景

Ted*_*y13 89 uitableview ios

我意识到iOS 7还没有正式发布,我们不应该讨论它,但我会疯狂地试图找出这个问题.在iOS 6上,我的表视图是透明的,看起来很棒.第一次运行iOS 7,背景为白色.

我已经尝试将表backgroundColor,单元格颜色等制作成UIColor clearColor但没有变化.

如何解决这个问题?

桌子

sla*_*mor 121

    // Fix for iOS 7 to clear backgroundColor
    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView = [[UIView new] autorelease];
    cell.selectedBackgroundView = [[UIView new] autorelease];
Run Code Online (Sandbox Code Playgroud)

在cellForRowAtIndexPath中

另外,请确保您的tableview实际上具有透明背景(在故事板中): 在此输入图像描述

  • 只需要添加行:cell.backgroundColor = [UIColor clearColor]; (11认同)

Max*_*ion 58

把这个:

cell.backgroundColor = [UIColor clearColor];
Run Code Online (Sandbox Code Playgroud)

在这个部分:

cellForRowAtIndexPath
Run Code Online (Sandbox Code Playgroud)


小智 25

尝试先将backgroundView设置为nil.

[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundColor:[UIColor clearColor]];
Run Code Online (Sandbox Code Playgroud)

不确定这是iOS7文档中的更改还是一直存在并且只是没有影响背景颜色,但是每个UITableView类参考@property backgroundView

"您必须将此属性设置为nil才能设置表格视图的背景颜色."

编辑:更正的代码语法


nab*_*nab 25

这已得到回答,但在很多方面都是错误的.

您需要实现以下委托方法:

    - (void)tableView:(UITableView *)tableView  willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cell setBackgroundColor:[UIColor clearColor]];
}
Run Code Online (Sandbox Code Playgroud)

你不能把修复中的cellForRowAtIndexPath,因为这是细胞之后被渲染,在此之前的背景被设置为透明(在较慢的设备),它会闪烁白色背景.

使用此委托方法,您的问题就解决了!


小智 14

实际上根据文档(UITableViewCell类参考),改变单元格背景颜色的官方正确位置是不同的:

无论使用预定义单元格还是自定义单元格,都可以使用backgroundView属性或更改继承的backgroundColor属性来更改单元格的背景.在iOS 7中,单元格默认为白色背景; 在早期版本的iOS中,单元格继承封闭表视图的背景颜色.如果要更改单元格的背景颜色,请在表视图委托的tableView:willDisplayCell:forRowAtIndexPath:方法中执行此操作.


Ber*_*erk 6

这是一个非常令人沮丧的问题.这是我目前的解决方案:

将其添加到您的UITableViewCell子类.

- (void)didMoveToSuperview {
    [super didMoveToSuperview];

    self.backgroundColor = [UIColor clearColor];
}
Run Code Online (Sandbox Code Playgroud)


Jam*_*ayq 6

斯威夫特3,4

cell.backgroundColor = UIColor.clear
Run Code Online (Sandbox Code Playgroud)


Dev*_*evC 5

这在iOS7 +中对我有用:

self.tableView.backgroundColor =[UIColor blueColor];
self.tableView.opaque = NO;
self.tableView.backgroundView = nil;`
Run Code Online (Sandbox Code Playgroud)

然后在 cellForRowAtIndexPath:

cell.backgroundColor = [UIColor clearColor];
Run Code Online (Sandbox Code Playgroud)


Sol*_*uiD 5

仅当我为每个单元格编辑清晰的背景颜色并为表格本身编辑清晰的颜色时,这才对我有用。两者都以编程方式

设置表格的清晰颜色:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    initMenu()
    myTableView.backgroundColor = UIColor.clearColor()
}
Run Code Online (Sandbox Code Playgroud)

设置单元格的颜色:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("tablecellid", forIndexPath: indexPath)
    cell.backgroundColor = UIColor.clearColor()
    return cell
}
Run Code Online (Sandbox Code Playgroud)