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

标签 ios swift uitableview tableview

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

enter image description here

如何设置.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/

相关文章:

ios - nil 导入带有电子邮件地址的联系人时验证失败

ios - 使用 iOS 的 UITableView,如果单元格在图像下载到其中之前已经出队怎么办?

ios - 将数据从嵌入式 PageViewController 传递到父 View Controller

iphone - NSURLConnection 异步间歇性地返回 null

ios - 标签文本和导航栏标题更改不会在 iOS 模拟器中更新。

arrays - 如何在 Swift 中截断字符串数组?

ios - 如何根据UITableView的内容高度设置高度?

ios - 新手: about ios/objective -c variable definition in a class

ios - Alpha 没有改变 UINavigationBar 的 barTintColor

ios - 触摸物体内部的位置