我想将UITableViewCell
的背景色设为透明,但似乎无效。在tableViewCell
中有一些图像/按钮,我希望让白色grouptableview
背景消失,使图像和按钮具有“浮动”效果(就好像它们不在tableview
中一样)。
有什么方法可以实现这个效果吗?
我想将UITableViewCell
的背景色设为透明,但似乎无效。在tableViewCell
中有一些图像/按钮,我希望让白色grouptableview
背景消失,使图像和按钮具有“浮动”效果(就好像它们不在tableview
中一样)。
有什么方法可以实现这个效果吗?
如果之前的两个选项都没有起作用,尝试这个
UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
这个问题实际上在UITableViewCell的文档中有解答。所以尽管您必须设置控件的透明度,但实际的单元格背景颜色必须按照以下方式设置。
“注意:如果您想通过设置UIView声明的backgroundColor属性更改单元格的背景颜色,则必须在委托的tableView:willDisplayCell:forRowAtIndexPath:方法中进行,而不是在数据源的tableView:cellForRowAtIndexPath:方法中进行。在分组样式表视图中更改单元格的背景颜色对iOS 3.0产生的影响与操作系统先前版本不同。它现在会影响圆角矩形内部的区域,而不是外部的区域。”
https://developer.apple.com/documentation/uikit/uitableviewcell
首先阅读以下文章以正确设置背景颜色:
http://pessoal.org/blog/2009/02/25/customizing-the-background-border-colors-of-a-uitableview/
然后尝试以下所有代码行:
[[cell contentView] setBackgroundColor:[UIColor clearColor]];
[[cell backgroundView] setBackgroundColor:[UIColor clearColor]];
[cell setBackgroundColor:[UIColor clearColor]];
一个UITableViewCell中不止有一个视图被隐藏了。这会让你看到所有挡在你面前的视图。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor clearColor];
cell.backgroundView.backgroundColor = [UIColor clearColor];
}
我有一个简单的解决方案
Objective-c 版本:
cell.backgroundColor = [UIColor clearColor];
Swift版本:
cell.backgroundColor = UIColor.clearColor()
默认情况下,UITableViewCell 的背景是白色的。执行 myCell.backgroundColor = [UIColor clearColor];
会使 UITableViewCell 变成透明,但你不会感觉到区别,因为 UITableView(UITableViewCell 的容器)也具有默认的白色背景。
如果你想看到 UIView 的背景,你需要将单元格和表格的背景都设置为透明:
myTableView.backgroundColor = [UIColor clearColor];
myTableCell.backgroundColor = [UIColor clearColor];
Martin Magakian
我曾经遇到这样一个问题:自定义的tableviewcell背景图片被白色标签背景遮盖,我尝试了很多方法,最后在viewWillAppear中将背景颜色设置为透明色解决了问题。
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.tableView.backgroundColor = [UIColor clearColor]; // if this is not here, cell background image is covered with a white rectangle :S
}
在rowForCellAtIndexPath中,我设置了背景图片:
if(!cell) {
cell = [[[UITableViewCell alloc] initWithFrame: ...
UIImageView *cellBG = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellbg.jpg"]];
[cell setBackgroundView:cellBG];
[cellBG release];
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
tableView.backgroundColor = UIColor.clear
return 40
}
然后在你的cellForRowAt中添加:
cell?.backgroundColor = UIColor.clear
这样它看起来就像:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell?.backgroundColor = UIColor.clear
return cell!
}
就是这样。当它工作时,我简直不敢相信自己的眼睛。:)
有3个步骤:
cell.backgroundView.hidden = YES
没有。 - Kevin Borders