How can we inherit the UITableViewDataSource protocol in our class?

enter image description here

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

+4
source share
2 answers

This is not the right way to determine protocol compliance. This is the correct syntax.

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

If you still see this error, it is because your class does not implement the required methods from these delegates, in this case numberOfRowsInSection .... and cellForRowAtIndexPath ....

+3
source

, , , , :

class MainViewController: UIViewController {

   // ...

}

extension MainViewController: UITableViewDataSource {

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

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

    // ... 

}

extension MainViewController: UITableViewDelegate {

    // ...

}
+2

Source: https://habr.com/ru/post/1544437/


All Articles