Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 579
senty Avatar asked Dec 05 '15 00:12

senty


2 Answers

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.

like image 189
R P Avatar answered Nov 10 '22 13:11

R P


There are at least 2 conformations in your class. You need to extend only once.

First Scenario:

You conform in the class description AND in the extension.

class MyViewController: MyDelegate{
    //class functions here
}

extension MyViewController: MyDelegate{
func1()
}

Remove "My Delegate" in the class description.

class MyViewController{
    //class functions here
}

extension MyViewController: MyDelegate{
func1()
}

Second Scenario:

You conform in two extensions.

extension MyViewController: MyDelegate{
func1()
}

extension MyViewController: MyDelegate{
func2()
}

Merge them into one extension like:

extension MyViewController: MyDelegate{
func1()
func2()
}
like image 27
Onat Korucu Avatar answered Nov 10 '22 13:11

Onat Korucu