Reputation: 697
I want to make a custom UITableViewCell that can have many columns. One will be a UILabel and one will be a UIButton. I want to start this off by saying I only create my IBOutlets by holding the control key and dragging the UI items to my code. So this means all of my IBOutlets are connected properly. My problem is that the UILabel and the UIButton are not visible in any of my cells. The cells are there, but nothing else.
class CustomTableViewCell: UITableViewCell{
@IBOutlet weak var label: UILabel!
@IBOutlet weak var button: UIButton!
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
var items: [Strings] = ["one", "two", "three"]
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad(){
super.viewDidLoad()
self.tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "cell")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:CustomTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "cell")! as! CustomTableViewCell
cell.label?.text = self.items[indexPath.row]
return cell
}
}
I have the UITableView's data source and delegate set to the ViewController, I have the cell reuse id set up properly and both the UILabel and UIButton have constraints set up. I feel like I am missing something.
Upvotes: 0
Views: 950
Reputation: 285049
You must not register the cell – when using prototype cells – but you must reload the table view.
Replace
override func viewDidLoad(){
super.viewDidLoad()
self.tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "cell")
}
with
override func viewDidLoad(){
super.viewDidLoad()
self.tableView.reloadData()
}
Upvotes: 1
Reputation: 2099
Those are missing:
self.tableView.delegate = self
self.tableview.datasource = self
Hope this helps!
Upvotes: 0