【发布时间】:2016-11-18 13:35:13
【问题描述】:
如何使 UITableView 和它的单元格在 Swift 3 中清晰。
我已经完成了之前的线程,但我仍然得到白色背景。
从我的代码中可以看出,我已经尝试了提到的各种方法:
override func viewDidLoad() {
self.communitiesTableView.delegate = self
self.communitiesTableView.dataSource = self
let background = CAGradientLayer().bespokeColor()
background.frame = self.view.bounds
// view.addSubview(background)
super.viewDidLoad()
// Do any additional setup after loading the view.
}
在我的单元格表函数中:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let title = self.communities[indexPath.row]
let cell = UITableViewCell()
cell.textLabel?.text = title
cell.textLabel?.font = UIFont(name: "Avenir", size: 12)
cell.textLabel?.textColor = UIColor.red
cell.textLabel?.backgroundColor = UIColor.clear
cell.contentView.backgroundColor = UIColor.clear
communitiesTableView.backgroundColor = UIColor.clear
cell.layer.backgroundColor = UIColor.clear.cgColor
return cell
}
这个 gif 显示了问题 - 请注意显示表格的黑线只是没有填充(因为没有人登录)
但有一秒钟它是清晰的,然后变成白色。 我哪里错了?
这是我发现的另一篇文章:
苹果文档说
... 在 iOS 7 中,单元格默认为白色背景;在早期版本的 iOS 中,单元格继承了封闭表格视图的背景颜色。如果要更改单元格的背景颜色,请在表格视图委托的 tableView:willDisplayCell:forRowAtIndexPath: 方法中进行。
您可能需要使用 willDisplayCell UITableView 委托方法为您的表格视图设置透明背景。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cellforRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
}
我如何应用上面的代码,因为它说它适用于 iOS 7?
【问题讨论】:
-
代码看起来不错。对于调试,设置
cell.textLabel.backgroundColor = UIColor.clear。如果问题仍然存在,则涉及另一种观点。在这种情况下,显示视图和单元格在情节提要中是如何堆叠的。这个:cell.isOpaque = false不需要,视图 alpha 与您的问题无关。 -
添加人的建议后问题依旧。我已经更新了代码以反映这一点。该表包含在 StackView 中。如果我从这个 StackView 中删除表,问题仍然存在......
-
我已将表格从 StackView 中取出,但问题仍然存在。
-
在
cellForRowAt indexPath中尝试一次cell.backgroundColor = .clear。 ` -
清晰约一秒钟,然后变白
标签: ios swift uitableview swift3