Async-
Async-

Reputation: 3289

Xib file UITableViewCell outlets are nil

This is a bit of an ongoing topic, but my situation is slightly different. I'm working with this tutorial. I have a view controller, that has it's own storyboard, this view controller has table view. The view controller is this table's delegate and data source. I need to add different kinds of cells to this table, and I'm using cell view model as well for this.

Cell:

class TitleTextCell: UITableViewCell, CellConfigurable {
        
        @IBOutlet weak var titleLabel: UILabel!
        @IBOutlet weak var textView: UITextView!
        static let identifier = "TitleTextCell"
        
        
        func setup(viewModel: RowViewModel) {
            guard let titleTextViewModel = viewModel as? TitleTextViewModel else {return}

            titleLabel.text = titleTextViewModel.title //fatal: found nil while 
            textView.text = titleTextViewModel.text    //unwrapping an optional value
        }
    }

Table view controller's ViewController:

class InfoViewController: UIViewController, Storyboarded {
        // MARK: - Properties
        var viewModel: InfoViewModelType! {
            didSet {
                viewModel.viewDelegate = self
            }
        }
        
        // MARK: - Outlets
        @IBOutlet weak var tableView: UITableView!
        
        // MARK: - Lifecycle Methods
        override func viewDidLoad() {
            super.viewDidLoad()
            tableView.register(TitleTextCell.self, forCellReuseIdentifier: TitleTextCell.identifier)
            tableView.delegate = self
            tableView.dataSource = self  
        }
    }

    extension InfoViewController: UITableViewDelegate, UITableViewDataSource {
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return viewModel.tableRows.count
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let rowViewModel = viewModel.tableRows[indexPath.row]
            let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier(for: rowViewModel), for: indexPath)
            if let cell = cell as? CellConfigurable {
                cell.setup(viewModel: rowViewModel)
            }
            return cell
        }
        
        private func cellIdentifier(for viewModel: RowViewModel) -> String {
            switch viewModel {
            case is TitleTextViewModel:
                return TitleTextCell.identifier
            default:
                fatalError("Unexpected view model type: \(viewModel)")
            }
        }
    }

The cell is a xib file. Outlets are connected with the file owner (see screens). enter image description here

enter image description here It does arrive at the point of func setup(viewModel: RowViewModel) which means table-wise it's correct. But the outlets are nil at runtime, what am I missing?

Upvotes: 0

Views: 813

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100533

You need

tableView.register(UINib(nibName: "TitleTextCell", bundle: nil), forCellReuseIdentifier: TitleTextCell.identifier)

For xib cells

Upvotes: 5

Related Questions