UITableView Programmed with UITableViewCell in Storyboard

0

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:

  • I create a new View and in the storyboard I put a Table View Cell element as the base element. About the place the elements that make up my cell.
  • I make sure the cell has an identifier
  • 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.

        
    asked by Salvador Nava 20.06.2018 в 09:22
    source

    1 answer

    0

    I think the problem is that instead of registering the class of the cell, you must register the nib since you are using .xib s. You can do it like this:

    let nib = UINib(nibName: "ReviewCellTVC", bundle: nil)
    tableView.register(nib, forCellReuseIdentifier: "reviewCell")
    

    Removing the line from registering the cell gives you an error because registering the cell is necessary because you are not using storyboards.

    However, what is the reason why you create the UITableView programmatically and the cell in a .xib ? The two things can be done in a storyboard with a prototype tableView and very easily.

        
    answered by 20.06.2018 / 09:36
    source