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.
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....
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 {
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With