ios UITableViewCell 显示白色背景,在 iOS7 上无法修改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18878258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITableViewCell show white background and cannot be modified on iOS7
提问by Kjuly
I've implemented a custom table view cell class that inherit from UITableViewCell
. The tableview contains a background image, so I want cell's background to be transparent. It looks great before iOS7.
我已经实现了一个从UITableViewCell
. tableview 包含一个背景图像,所以我希望单元格的背景是透明的。在iOS7之前看起来很棒。
However, in iOS7, the cell is always shown with a white background.
但是,在 iOS7 中,单元格始终显示为白色背景。
Even for Xcode7, 2015, there is a bug in storyboard: you have to set the background color of a cell in code.
即使对于 Xcode7, 2015,storyboard 中也存在一个错误:您必须在代码中设置单元格的背景颜色。
回答by Kjuly
As Apple DOC said (UITableViewCell Class Reference):
正如 Apple DOC 所说(UITableViewCell 类参考):
... In iOS 7, cells have a white background by default; in earlier versions of iOS, cells inherit the background color of the enclosing table view. If you want to change the background color of a cell, do so in the tableView:willDisplayCell:forRowAtIndexPath:method of your table view delegate.
...在 iOS 7 中,单元格默认为白色背景;在早期版本的 iOS 中,单元格继承了封闭表格视图的背景颜色。如果要更改单元格的背景颜色,请在表视图委托的tableView:willDisplayCell:forRowAtIndexPath:方法中执行此操作。
So for my case that to show cells with transparent background, just need to implement the delegate method in the table view controller like below:
因此,对于我的情况,要显示具有透明背景的单元格,只需要在表视图控制器中实现委托方法,如下所示:
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
}
Just Note: As @null said, "...there seems to be a bug in interface builder...", I'm not totally sure whether it does have the bug, but seems so cause his comment got several up votes. So there might something wrong if you use IB. :)
请注意:正如@null 所说,“......界面构建器中似乎有一个错误......”,我不完全确定它是否确实有这个错误,但似乎是因为他的评论得到了好几票。因此,如果您使用 IB,可能会出现问题。:)
回答by Anton Filimonov
I investigated it a little bit and found out that the cell backgroundColor is set by the Appearance system. So if all cells in your application have clear background, the easiest solution would be:
我稍微调查了一下,发现单元格背景颜色是由外观系统设置的。因此,如果应用程序中的所有单元格都有清晰的背景,最简单的解决方案是:
[[UITableViewCell appearance] setBackgroundColor:[UIColor clearColor]];
And even if they have different background, clear color seems to be the most convenient as the default one.
即使它们有不同的背景,清晰的颜色似乎是最方便的默认颜色。
回答by sanjana
Write this in your cellForRowAtIndexPath method before return cell;
在返回单元格之前将其写入您的 cellForRowAtIndexPath 方法中;
cell.backgroundColor = [UIColor clearColor];
回答by Yiming Tang
The default background color of an UITableViewCell
in iOS 7 is white.
UITableViewCell
iOS 7中 an 的默认背景颜色是白色。
You have to set the backgroundColor
property somewhere in your code. For example, set it after you newly created the cell.
您必须backgroundColor
在代码中的某处设置该属性。例如,在您新创建单元格后设置它。
cell.backgroundColor = [UIColor clearColor];
回答by Endama
I actually found that the issue arose from the UITableView's Background color not being set to clear. If you change the UITableViewCell's Background color to clear and you are finding you still see white, make sure that the UITableView's background color is set to clear (or whatever you want).
我实际上发现问题是由于未将 UITableView 的背景颜色设置为清除而引起的。如果您将 UITableViewCell 的背景颜色更改为 clear 并且您发现您仍然看到白色,请确保将 UITableView 的背景颜色设置为 clear (或您想要的任何颜色)。
[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundColor:[UIColor clearColor]];
In Swift
在斯威夫特
tableView.backgroundView = nil
tableView.backgroundColor = UIColor.clearColor()
回答by superarts.org
This is definitely an iOS bug. In iOS 8, setting background color in Interface Builder
works fine for iPhone, but in iPad it's always whiteColor
. To fix it, in the cellForIndexPath
data source message, putting this in:
这绝对是一个iOS错误。在 iOS 8 中,在Interface Builder
iPhone 中设置背景颜色效果很好,但在 iPad 中它总是whiteColor
. 要修复它,在cellForIndexPath
数据源消息中,将其放入:
cell.backgroundColor = cell.backgroundColor
cell.backgroundColor = cell.backgroundColor
And it will work. This is exactly why I said it's a bug since the code itself is pretty much bullsh*t, but it works.
它会起作用。这正是我说这是一个错误的原因,因为代码本身几乎是废话*t,但它确实有效。
回答by abbood
It could be that your UITableViewCell is selected by default.. You can confirm this using Xcode 6s new visual debugger(or find out exactly which view is causing this white cell to appear).
可能是您的 UITableViewCell 被默认选中。您可以使用 Xcode 6s 新的可视化调试器确认这一点(或找出导致此白色单元格出现的确切视图)。
Interestingly, after knowing this.. setting the background color of the selected cell to clear stilldidn't work.. doing some more research, turns out i just had to modify the selection style when i'm creating this custom cell:
有趣的是,在知道这一点之后.. 将所选单元格的背景颜色设置为清除仍然不起作用.. 做一些更多的研究,结果我只需要在创建这个自定义单元格时修改选择样式:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// do customization here
}
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self setBackgroundColor:[UIColor clearColor]];
return self;
}
回答by Mike
I found that none of the above worked from XCode 5.1.1, iOS 7.1.
我发现在 XCode 5.1.1、iOS 7.1 上以上都不起作用。
If you are using Interface Builder with prototype cells, select the prototype cell, then from the attributes selector in the View section, change the Background form default to Clear Color:
如果您使用带有原型单元格的界面生成器,请选择原型单元格,然后从视图部分的属性选择器中,将背景表单默认更改为清除颜色:
This seems to work fine. None of the above code changes are needed, either--this is a pure IB solution.
这似乎工作正常。也不需要更改上述任何代码——这是一个纯粹的 IB 解决方案。
回答by RajV
The accepted answer did not fix the problem for me. I had to do this:
接受的答案并没有为我解决问题。我不得不这样做:
- In the interface builder, select the table view. Then from the attributes inspector, from the Viewsection, set the Backgroundto transparent (0% opacity).
- 在界面构建器中,选择表视图。然后从属性检查器的视图部分,将背景设置为透明(0% 不透明度)。
From the table's data source class cellForRowAtIndexPath method:
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.backgroundColor = [UIColor clearColor]; //Make cell transparent
从表的数据源类 cellForRowAtIndexPath 方法:
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.backgroundColor = [UIColor clearColor]; //Make cell transparent
回答by mllm
For me setting
cell.backgroundColor = cell.contentView.backgroundColor;
either in tableView:willDisplayCell:forRowAtIndexPath:
or tableView:cell:forRowAtIndexPath:
did the job.
对我来说,设置
cell.backgroundColor = cell.contentView.backgroundColor;
在任一tableView:willDisplayCell:forRowAtIndexPath:
或tableView:cell:forRowAtIndexPath:
做的工作。
This is because I set the contentView's background colour in Interface Builder as desired.
这是因为我根据需要在 Interface Builder 中设置了 contentView 的背景颜色。