Search code examples
xcodeswiftuitableviewxib

Redundant conformance of TableView to protocol UITableViewDataSource with Xib Files


I have a parent view UIViewController (on storyboard), a TableViewController with .xib and TableViewCell with .xib. I am trying to connect DataSource to the TableView however it's giving me an error:

Redundant conformance of 'TableView1' to protocol 'UITableViewDataSource'

'TableView1' inherits conformance to protocol 'UITableViewDataSource' from superclass here.

Without adding dataSource near class and try it as class TableView1: UITableViewController {.. , it doesn't give me any error and in the simulator, I can see the table view illusion when I scroll down.

However, when I try to add dataSource, it gave me these errors.

The path I followed on setting it up...:

  1. Ctrl + drag from xib to TableView1 and connected it as Globals

  2. In xib file, I connected DataSource & Delegate

enter image description here

  1. Finally, my TableView1:

class TableView1: UITableViewController, UITableViewDataSource { error here..

@IBOutlet var GlobalsTableView: UITableView!

var results: [AnyObject]? = []

override func viewDidLoad() {
    super.viewDidLoad()
        print("A")
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return self.results?.count ?? 0
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! DogTableViewCell

    return cell
 }
}

Please note that in TableView1.xib, I can't select TableView1 as Custom Class -> Class (but i don't think it's necessary).


Solution

  • When a class inherits from UITableViewController, it by default conforms to UITableViewDataSource & UITableViewDelegate and you need not explicitly specify it.

    You need to conform to UITableViewDataSource and UITableViewDelegate only when you embed a UITableView in a UIViewController.