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

问题描述 投票:0回答:1

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

enter image description here

如何设置.backgroundcolor,使其清晰如图片所示?我遇到的另一个问题是我的TableView被“预填充”为空的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
}

自定义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的所有功能均正常工作。只是backgroundcolor和空的cells ...

我非常感谢您的帮助:)我对此一无所获。。

我的UITableView有问题。最终,我希望它看起来像这样(白色矩形)。如何设置.backgroundcolor使其清晰可见(如图片所示)?我是另一个问题...

ios swift uitableview tableview
1个回答
0
投票

在您的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")
}
© www.soinside.com 2019 - 2024. All rights reserved.