Reputation:
I am having a problem with my UITableView
. Eventually I want it to look like this (the white rectangle).
How can I set .backgroundcolor
so it is clear, like in the picture??
Another problem I am having is that my TableView
is "pre-filled" with empty cells
..
I've tried this but somehow it is not working:
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
}
Custom Cell
class
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.addSubview(checkButton)
self.backgroundColor = .clear
}
How I create my 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
}()
My UITableView
is working fine with all its functions. It's just the backgroundcolor
and the empty cells
...
I am grateful for any help :) I couldn't find anything on this..
Upvotes: 0
Views: 1563
Reputation: 77462
In your 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")
}
Now you no longer need v.view.backgroundColor = .clear
in your declaration.
Another option if, for example, you sometimes want to use an instance of WhishlistTableViewController
without the transparent background, change your declaration from:
let theTableView: WhishlistTableViewController = { ... }
to:
lazy var theTableView: WhishlistTableViewController = { ... }
Upvotes: 1