我的 UITableView
遇到问题。最终我希望它看起来像这样(白色矩形)。
如何设置.backgroundcolor
使其清晰,如图所示?
我遇到的另一个问题是我的 TableView
已“预填充”了空的单元格
..
我已经尝试过,但不知何故它不起作用:
TableViewController
类
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: WhishCell.reuseID, for: indexPath)
let currentWish = self.wishList[indexPath.row]
cell.textLabel?.text = currentWish.wishName
cell.backgroundColor = .clear
return cell
}
自定义Cell
类
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.addSubview(checkButton)
self.backgroundColor = .clear
}
如何创建我的 UITableView
let theTableView: WhishlistTableViewController = {
let v = WhishlistTableViewController()
v.view.layer.masksToBounds = true
v.view.layer.borderColor = UIColor.white.cgColor
v.view.backgroundColor = .clear
v.view.layer.borderWidth = 2.0
v.view.translatesAutoresizingMaskIntoConstraints = false
return v
}()
我的 UITableView
的所有功能都运行良好。它只是背景颜色
和空的单元格
...
我很感激您的帮助:)我找不到任何相关内容..
最佳答案
在您的 WhishlistTableViewController
类中:
override func viewDidLoad() {
super.viewDidLoad()
// set background to clear
tableView.backgroundColor = .clear
// add clear view as tableFooterView
// to prevent empty cells
let v = UIView()
v.backgroundColor = .clear
tableView.tableFooterView = v
// ... rest of table setup
tableView.register(WishCell.self, forCellReuseIdentifier: "WishCell")
}
现在您的声明中不再需要 v.view.backgroundColor = .clear
。
另一个选项,例如,如果您有时想使用 WhishlistTableViewController
的实例没有透明背景,请将您的声明更改为:
let theTableView: WhishlistTableViewController = { ... }
到:
lazy var theTableView: WhishlistTableViewController = { ... }
关于ios - Swift - 在 UITableView 中设置清晰的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59054848/