Search code examples
iosswiftuitableviewxib

How do I add the cell I created in different xib to the table view I created in the uiview controller?


I added tableview to uiview controller, then I created uitableview cell in another xib. I want to show this cell in tableview. But the cell comes empty. I cant create a prototyp cell in xib. The first code blog belongs to vc and the other to the cell I use.

final class ViewController: BaseViewController {
    private let viewModel = ViewModel()
    private let router = Router()

    // MARK: - IBOutlets
    @IBOutlet weak var infoLabel: UILabel!
    @IBOutlet weak var prefenceButton: BorderedButton!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var statusImageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        self.tableView.register(TableViewCell.self, forCellReuseIdentifier: "Cell")
    }
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as!     TableViewCell
        return cell
    }
}
class TableViewCell: UITableViewCell, ReusableCell {
    static var height: CGFloat = UITableView.automaticDimension
    static var estimatedHeight: CGFloat = 150

    var router = Router()

    var viewModel = ViewModel()
    @IBOutlet weak var slipPrefenceLabel: UILabel!
    @IBOutlet weak var showAllMerchantLabel: UILabel!
    @IBOutlet weak var merchantLabel5: UILabel!
    @IBOutlet weak var merchantLabel4: UILabel!
    @IBOutlet weak var merchantLabel3: UILabel!
    @IBOutlet weak var merchanLabel2: UILabel!
    @IBOutlet weak var merchantLabel1: UILabel!

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

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

    @IBAction func tapShowAllMerchantLabel(_ sender: UITapGestureRecognizer) {
        router.routeToSelectedMerchantList()
    }
}

Solution

  • put this in view did load

    self.tableView.register(UINib.init(nibName: "TestTableviewCell1", bundle: nil), forCellReuseIdentifier: "TestTableviewCell1")
    

    put this code

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
        let cell = tableView.dequeueReusableCell(withIdentifier: "TestTableviewCell1", for: indexPath) as! TestTableviewCell1
    
        return cell
    }
    

    create class for custom cell

    class TestTableviewCell1: UITableViewCell{
        
        @IBOutlet weak var titleLbl: UILabel!
        @IBOutlet weak var checkBtn: UIButton!
        
    }
    

    and create xib I have share screenshot of xib as well.

    enter image description here

    enter image description here