What does below paragraph mean , can any one please explain

  1. What data source link is the author talking about

2)Again what table view is the author referring to here

3)If the table view wants to know how many rows of data it has , from where it will know and which table view is it , is it. the method table view or the arguments inside. the method table view

  1. Once it is hooked up to a data source – i.e. your view controller – the table view , this line is very confusing which data source got hooked to which tableview , how is the controller doing all this ?

“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 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[…]”

Just start with something simple.

You put a UITableViewController on your storyboard, and it contains a UITableView. So click Run. What does it display? Not much, not even any rows.

How do you get it to display some rows? Well, it asks you how many you want. You just need to tell it. So you add this piece of code:

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

Now click Run, and it will display 5 rows. So your code is providing the data, in response to questions from the UITableView that is contained in the UITableVIewController that you put on your storyboard.

1 Like

@sgerrard - thanks I did as you said and indeed it does not show any thing at start. just white screen, so when I add

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
  }
  1. This method is inside - UITableViewController , which is a protocol , am I right ?

  2. And then this method ends up sending information to the view or the Main.storyboard , am I right ?

  3. So the table view methods are a way for the controller to pass information , am I right

But what is the delegate here ?

Also why are we mentioning _ tableView: UITableView inside the tableView method and how is it able to ask question , is it a class ?

Thanks a lot that was very helpful , if only the author was writing this way …

Amit

This topic was automatically closed after 166 days. New replies are no longer allowed.