Group Group Group Group Group Group Group Group Group

Some confusion in chapter 9 of iOs Apprentice


#1

Hi,

Can any one please explain

  1. What is a protocol methods

2)What is a datasource here , I never added any data so where does it come from ?

  1. What is the table view here , the design side table view, the table view methods that are repeated again and again

  2. They use tableview word so many times in so many context that I do not. know what it is ?

Instead, you say to the table view: “This view controller is now your data source. You can ask it questions about the data anytime you feel like it.”

Once it is hooked up to a data source – i.e. your view controller – the table view sends a numberOfRowsInSection message to find out how many data rows there are.

And when the table view needs to draw a particular row on the screen it sends a cellForRowAt message to ask the data source for a cell.

what table view the author is refering to ? It would have been really nice if they had told clearly …

Is it this table view ?

 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 100
    }

or some thing else …

The more I read the more confusing everything is getting below are the paragraphs that have confused me even more —

Kindly guide

The above two methods are part of UITableView’s data source protocol.

What's a protocol, you ask? Well, its a standard set of methods that a class must adhere to — a protocol to be followed, so to speak. It allows code to be written in such a way that you know that a given class would implement certain methods (with specific parameters of a given type) but where you don't need to know all the implementation details of the class — such as all it's methods. A protocol usually allows you to add functionality for a certain type of operation to a class — for example, handling data for a table view.

The data source is the link between your data and the table view. Usually, the view controller plays the role of data source and implements the necessary methods. So, essentially, the view controller is acting as a delegate on behalf of the table view. (This is the delegate pattern that we've talked about before — where an object does some work on behalf of another object.)

The table view needs to know how many rows of data it has and how it should display each of those rows. But you can’t simply dump that data into the table view’s lap and be done with it. You don’t say: “Dear table view, here are my 100 rows, now go show them on the screen.”

Instead, you say to the table view: “This view controller is now your data source. You can ask it questions about the data anytime you feel like it.”

Once it is hooked up to a data source – i.e. your view controller – the table view sends anumberOfRowsInSection message to find out how many data rows there are.

And when the table view needs to draw a particular row on the screen it sends acellForRowAt message to ask the data source for a cell.

You see this pattern all the time in iOS: one object does something on behalf of another object. In this case, the ChecklistViewController works to provide the data to the table view, but only when the table view asks for it.

#2

@amitsrivastava Please check out this thread when you get a chance:

I hope it helps!