Search code examples
iosswiftuitableviewstoryboardxib

Cannot load XIB into reusable TableViewCell


I created an XIB file and its controller to handle my reusable custom cells:

enter image description here

And set my XIB's class owner:

enter image description here

class WallTableCell: UITableViewCell {
    //Outlets
    
    func setupCell() {
        setupLabels()
    }
    
    func setupLabels() {
        self.title.text = "Sample Title"
        self.postDescriptionLabel.text = "Sample Description"
    }
}

I, currently have 2 storyboards, where the first one leads to the second one, using a NavigationView, the second one contains a TabBarView where the first of the items has a TableView.

Here, I added a UITableViewCell in the storyboard

enter image description here

The cell's class is set to:

enter image description here

And the controller's class is set to:

enter image description here

Where I register my Nib and try to use it on the cellForRowAt method:

class WallViewController: UIViewController {
    @IBOutlet weak var messagesTable: UITableView!
    
    override func viewDidLoad() {
        loadNibForCells()
    }
    
    func loadNibForCells() {
        let nib = UINib(nibName: "WallTableCell", bundle: nil)
        messagesTable.register(nib, forCellReuseIdentifier: "wallCell")
    }
}

extension WallViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let wallCell = messagesTable.dequeueReusableCell(withIdentifier: "wallCell") as? WallTableCell else {
            return UITableViewCell()
        }
        
        wallCell.setupCell()
        
        return wallCell
    }
}

But still, when I run the app in the simulator, I keep getting an empty cell.

enter image description here

What should I do in order to display my custom XIB view inside my tableViewCell?

After following @Sh_Khan's answer and adding:

messagesTable.delegate = self
messagesTable.dataSource = self

The app crashes with an error:

Thread 1: Exception: "[<NSObject 0x600001628e70> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key postDescriptionLabel"

I believe it has to do with the fact that the TableViewCell inside the storyboard is empty rather than containing the components of the WallTableCell


Solution

  • While I was missing to add my messagesTable's dataSource which was what Sh_Khan recommended, the solution for my error afterwards was that my reference outlets were pointing to "File's owner" rather than my File's outlets.

    So, when selecting the custom TableViewCell (not the prototype) should be:

    enter image description here

    For some reason, instead of being "Title Label" on the right side, it was "File's owner"

    And the prototype cell shouldn't contain any class attached to it.