【问题标题】:Adding cells to visible empty UITableView show up with white background将单元格添加到可见的空 UITableView 以白色背景显示
【发布时间】:2019-01-06 03:50:52
【问题描述】:

使用 Xcode 10+、Swift 4+、iOS 11+

我创建了一个顶部有一个 SearchBar 的简单搜索视图, 下面有一个 UITableView。

当视图第一次显示时,表格是空的,当用户在搜索栏中输入文本时,我将结果添加到表格中。功能上,一切正常。

接下来,我将主视图背景设为蓝色,并希望整个 TableView 清晰,以便我可以看到背景。

我已经在“viewDidLoad”中设置了表格视图背景:

myTable.backgroundColor = UIColor.clear

在“cellForRowAt”中,我将单元格背景设置为清除:

var cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "cell")
cell.backgroundColor = UIColor.clear
cell.contentView.backgroundColor = UIColor.clear

但是,当搜索返回值并将单元格添加到表中时, 它们总是有白色背景而不是清晰的。

如果我在不清除表格的情况下弹出然后推送搜索视图,现有表格单元格会按照我想要的方式显示(重绘) - 清除。

我也尝试将其添加到“didFinishLaunchingWithOptions”,但没有帮助:

UITableViewCell.appearance().backgroundColor = UIColor.clear

如何使单元格在首次创建时显示清晰?

【问题讨论】:

  • 当您查看视图层次结构调试器时,究竟什么是白色?
  • @Mocha 我查看了层次结构并发现了问题 - 我使用的是 SearchBar 控制器而不是 SearchBar - 所以我有两个 TableViews 出现在层次结构中,一个属于 Search 的是问题。我现在已更改为搜索栏(仅)并且它正在工作。谢谢,呵呵!

标签: ios swift uitableview


【解决方案1】:

尝试在单元格显示时设置单元格或表格视图的背景颜色。

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.backgroundColor = UIColor.clear
}

或者你可以覆盖函数setBackgroundView来调试问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    相关资源
    最近更新 更多