Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we inherit UITableViewDataSource protocol in our class?

Tags:

swift

xcode6

ios8

enter image description here

I want to use table view in my application. But when i confirm my view controller to UITableViewDataSource, it gives error shown in image. How to use UITableViewDataSource protocol in our class.

like image 947
Rajesh Maurya Avatar asked Jun 13 '14 08:06

Rajesh Maurya


2 Answers

That's not the proper way to specify protocol conformance. This is the proper syntax.

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    // stuff
}

If you're still seeing that error, it's because your class doesn't implement the required methods from these delegates, in this case, numberOfRowsInSection.... and cellForRowAtIndexPath....

like image 81
Mick MacCallum Avatar answered Nov 10 '22 12:11

Mick MacCallum


I would recommend to use class extensions for conforming a protocol, that kind of isolation makes your code more maintainable later when your class has grown up, like e.g.:

class MainViewController: UIViewController {

   // ...

}

and you can extend it with the protocols

extension MainViewController: UITableViewDataSource {

    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
        return 0
    }

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        return nil
    }

    // ... 

}

and

extension MainViewController: UITableViewDelegate {

    // ...

}
like image 21
holex Avatar answered Nov 10 '22 11:11

holex