Swift - 在 UITableView 中设置清晰的背景颜色

Swift - set clear background color in UITableView

我的 UITableView 有问题。最终我希望它看起来像这样(白色矩形)。

如何设置.backgroundcolor像图片一样清晰?? 我遇到的另一个问题是我的 TableView 是 "pre-filled" 并且 cells..

我已经试过了,但不知何故它不起作用:

TableViewController class

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
}

自定义Cellclass

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 的所有功能都运行良好。只是 backgroundcolor 和空的 cells...

非常感谢您的帮助 :) 我在这方面找不到任何内容..

在你的 WhishlistTableViewController class:

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 = { ... }