I usually do the table-cell interfaces in swift programmatically. I generate my ViewController that has an instance of UITableView, in turn I generate another class that inherits from the UITableViewCell and I initialize it in the tableView: cellForRowAt.
What I want to do is continue to maintain the instance of the tableView programmatically, but create a cell prototype in the storyboard to be able to invoke it. I think I've achieved most of it, but I can not make it work:
In the main controller I add my instances of the table view controller.
var reviewsTableView: UITableView?
In the ViewdidLoad I add the code to instantiate my table, the name of my identifier reviewCell
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .gray
// Do any additional setup after loading the view.
reviewsTableView = UITableView(frame: view.frame)
reviewsTableView?.delegate = self
reviewsTableView?.dataSource = self
reviewsTableView?.separatorStyle = UITableViewCellSeparatorStyle.none
reviewsTableView?.register(ReviewCellTVC.self, forCellReuseIdentifier: "reviewCell")
view.addSubview(reviewsTableView!)}
In the cellForRowat function of the table I create an instance of my cell to reflash it
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = (tableView.dequeueReusableCell(withIdentifier: "reviewCell", for: indexPath) as? ReviewCellTVC)!
cell.nameLB?.text = "Chava Nava"
cell.backgroundColor = UIColor(named: "tableGray")
cell.selectionStyle = .blue
return cell
}
I have tried to unpacking my cell in different ways ( cell ) but it always comes back nil. In addition and trying not to register the class remove the line
reviewsTableView?.register(ReviewCellTVC.self, forCellReuseIdentifier: "reviewCell")
But in doing so, he tells me that he can not parse from UITableView to ReviewCellTVC
I know this is a very common practice, but all the examples I find talk about how to do both the tableView and the tableViewCell in the storyboard.