Abdullah Ajmal
Abdullah Ajmal

Reputation: 463

How to create a table view on a view controller when the view controller already inherits from a base view controller

I've got a view controller (TableViewController.swift) which inherits from a base view controller (BaseViewController.swift) like so:-

class TableViewController: BaseViewController {
    // Some code
}

I want to create a table view on my TableViewController but to do that you have to inherit from UITableViewController but I can't do that because Swift doesn't allow multiple inheritance; is there a way to solve this problem? I am new to Swift.

Upvotes: 2

Views: 395

Answers (1)

CristiCh
CristiCh

Reputation: 11774

A tableView is just a view underneath, and you can add it in any view controller you desire.

A tableViewController on the other hand is a viewController that comes with a built in tableView.

You don't need to subclass tableView only if you want to change its current functionality or appearance.

Just add a table view in your view controller and conform to UITableViewDataSource protocol by implementing its required methods (numberOfSections, numberOfRows and cellForRow..).

If you need to know when a cell is tapped or other actions on the tableView you should conform to UITableViewDelegate protocol also.

Upvotes: 6

Related Questions