Paul S.
Paul S.

Reputation: 1352

How to load a UITable with custom cell inside a UICollectionViewCell that is already a NIB

I have a UICollection view that has two custom cells. The UICollectionViewCell custom cells are built using NIB's. Inside one of the UICollectionViewCell NIB's, I have a UITableView. However, the UITableView itself has custom cells. So my question is:

How do I load a custom cell for the UITableView instead a UICollectionViewCell NIB?

import UIKit

class HeaderFooterCollectionViewCell: UICollectionViewCell {

    //Tableview
    @IBOutlet weak var tableView: UITableView!

    //Buttons
    @IBOutlet weak var resetButton: UIButton!
    @IBOutlet weak var periodButton: UIButton!
    @IBOutlet weak var undoButton: UIButton!


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code

        // Register cell classes
        tableView.register(UINib(nibName: "ShotInformationTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
    }

}

extension HeaderFooterCollectionViewCell: UITableViewDataSource {

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

    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! ShotInformationTableViewCell

        return cell
    }
}

Upvotes: 0

Views: 52

Answers (1)

Sudipto Roy
Sudipto Roy

Reputation: 6795

let cell =  Bundle.main.loadNibNamed("XIB_File_Name", owner: self, options: nil)?.first as! XIB_Class_Name

In almost all cases , XIB_File_Name == XIB_Class_Name

Upvotes: 1

Related Questions