Group Group Group Group Group Group Group Group Group

Difference between Datasource and delegate


#1

The author writes 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.)”

then there is a diagram later on —

So when he suggests that viewController plays role of data source and. then he says essentially, the view controller is acting as a delegate on behalf of the table view what is the difference , why are they shown as different in the image … kindly guide

Amit


#2

@amitsrivastava As you can see in the image, the view controller implements both the table view’s data source and delegate.


#3

For this app, and many others, the Table View Controller is going to be both the Data Source and the TableView Delegate. However, in some elaborate apps, the developer might want one object to be the Data Source, and another object to be the TableView Delegate. Making them separate gives the developer flexibility in how they arrange things.

The data source provides information about what to display, like how many rows, and what goes in each row. The delegate tells the tableview what to do when the user selects a row, or whether the user is allowed to edit a row, and other things like that.