Why are my table view cells not showing?

How do I use more than one table view cell subclass each with its xib file on a single table view? In my attempt, the cells are not showing. Here’s my code:

import UIKit

class DetailTableViewController: UITableViewController {

    let items = [0, 1]
    
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(DueDateSwitchTableViewCell.self, forCellReuseIdentifier: "DueDateSwitchTableViewCell")
        
        let xibDueDateSwitchTableViewCell = UINib(nibName: "DueDateSwitchTableViewCell", bundle: Bundle.main)
        
        tableView.register(xibDueDateSwitchTableViewCell, forCellReuseIdentifier: "DueDateSwitchTableViewCell")

        tableView.register(DueDatePickerTableViewCell.self, forCellReuseIdentifier: "DueDatePickerTableViewCell")
        
        let xibDueDatePickerTableViewCell = UINib(nibName: "DueDatePickerTableViewCell", bundle: Bundle.main)
        
        tableView.register(xibDueDatePickerTableViewCell, forCellReuseIdentifier: "DueDatePickerTableViewCell")
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        print("tableView(_:cellForRowAt:)", "indexPath.row=", indexPath.row)
        
        let cell = UITableViewCell()
        
        switch indexPath.section {
            
        case 0:
            
            print("\tcase 0")
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "DueDateSwitchTableViewCell", for: indexPath) as! DueDateSwitchTableViewCell
            
            cell.label.text = String(items[indexPath.section].hashValue)
            
            cell.backgroundColor = UIColor.yellow
            
        case 1:
            
            print("\tcase 1")
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "DueDatePickerTableViewCell", for: indexPath) as! DueDatePickerTableViewCell
            
            cell.label.text = String(items[indexPath.section].hashValue)
            
            cell.datePicker.date = Date()
            
        default:
            
            break
            
        }

        cell.textLabel!.text = String(items[indexPath.section].hashValue)
        
        return cell
        
    }

}

When I add the following code at the end of tableView(_:cellForRowAt:) before the return statement, the integer values show in the cells:

    cell.textLabel!.text = String(items[indexPath.section].hashValue)

In your code, you have three statements which begin let cell =. The first creates a UITableViewCell which I think is displaying on your screen. The other two are inside the scope of your switch statement, and when the switch statement ends, those cells are released from memory and never make it to the screen. The return cell line at the end of the function returns only the first cell you created, the only one outside the switch statement.

If you replace that first line with let cell : UITableViewCell, and remove the let from the let cells inside the switch, then there’ll only be one variable called cell, and the compiler will make sure that every path through the switch statement instantiates it before it’s returned at the end.

This topic was automatically closed after 166 days. New replies are no longer allowed.