ajrlewis
ajrlewis

Reputation: 3058

Adding a Table View Cell to a Table View within a View

I have a subclass of UIView (MyView) that I've hooked up to a NIB file.

class MyView: UIView {

    @IBOutlet var contentView: UIView!
    @IBOutlet weak var tableView: UITableView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.setup()
    }

    let nibName = "MyView"

    private func setup() {

        let bundle = Bundle.init(for: type(of: self))
        bundle.loadNibNamed(nibName, owner: self, options: nil)

        contentView.frame = self.bounds
        contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        self.addSubview(contentView)
    }

}

This view is simple; containing only one UITableView (or tableView). However, I want to add a UITableViewCell with a UILabel to tableView but the storyboard is not letting me do this.

I understand that MyView is not a view controller and therefore should not (if following an MVC pattern) implement the various table view data source / delate methods, but, still, why can I not add this table view cell to the table view within this custom view?

My aim was to then have some UIViewController subclass that has an instance of MyView, i.e.

var myView = MyView(),

which it then controls the datasource and delegate methods for, i.e.

myView.tableView.dataSource = self.

Finally, I've attached a screenshot showing that I am unable to add this table view cell to the table view.

enter image description here

Upvotes: 0

Views: 69

Answers (1)

DonMag
DonMag

Reputation: 77462

You cannot add prototype cells to table views in xib files - only in storyboards.

You can create your table view in one xib, and your table view cell in another xib, if you want.

Or, you can create a second storyboard that would contain a table view and it would support cell prototypes.

Upvotes: 1

Related Questions