skip to Main Content

I’m trying to populate my tableView with a custom cell – seems pretty straight forward, but for some reason when I run the following code, the custom cell doesn’t appear? I feel like I’m missing something super obvious.

Custom cell name is: OffersTableViewCell

DashboardViewController

class DashboardViewController: UIViewController, UITableViewDelegate, UITableViewDataSource  {

    
 @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.tableView.dataSource = self
        self.tableView.delegate = self
        
        self.registerTableViewCells()
    }

    func tableView(_ tableView: UITableView,
                   numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView,
                   cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: "OffersTableViewCell") as? OffersTableViewCell {
            return cell
        }
        
        return UITableViewCell()
    }
    
    private func registerTableViewCells() {
        let textFieldCell = UINib(nibName: "OffersTableViewCell",
                                  bundle: nil)
        self.tableView.register(textFieldCell,
                                forCellReuseIdentifier: "OffersTableViewCell")
    }
    
 

}

OffersTableViewCell

import UIKit

class OffersTableViewCell: UITableViewCell {

    @IBOutlet weak var offerTitle: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}

3

Answers


  1. I think you should be able to resolve the issue by using the code below. I would suggest we remove UITableViewCell().

    
        override func viewDidLoad() {
            super.viewDidLoad()
            
            self.tableView.dataSource = self
            self.tableView.delegate = self
            
            self.registerTableViewCells()
            self.tableView.reloadData()
        }
    
        func tableView(_ tableView: UITableView,
                       cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "OffersTableViewCell", for: indexPath) as! OffersTableViewCell 
            return cell
            //put any other exception conditions to display UITableViewCell() if necessary
        }
    
        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
    
    Login or Signup to reply.
  2. Make sure you have the Cell named – OffersTableViewCell.swift class, and OffersTableViewCell.xib that is having UI for that cell.

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.registerTableViewCells()
    
        self.tableView.dataSource = self
        self.tableView.delegate = self
    }
    
    func tableView(_ tableView: UITableView,
                   cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "OffersTableViewCell", for: indexPath) as? OffersTableViewCell else { return UITableViewCell() }
         
        cell.offerTitle.text = "blabla.."
    
        return cell
    }
    

    Please register cell before you assign the tableView delegate & datasource. Or just call reloadData after you register cell Nibs.

    Also, make sure you are loading safe data otherwise return default TableViewCell please.

    Login or Signup to reply.
  3. Your code works for me. Try to go through these steps:

    1. Check if .xib file named as OffersTableViewCell.xib
      enter image description here
    2. Check if File’s Owner class is OffersTableViewCell
      enter image description here
    3. Make sure you deleted default view in .xib and created UITableViewCell
    4. Check if class of cell is OffersTableViewCell
      enter image description here
    5. Check if cell identifier is OffersTableViewCell
      enter image description here
    6. Check if class of view controller in storyboard is DashboardViewController
    7. Make sure you open DashboardViewController in app
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search