Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I have to set the dataSource and delegate to self? [duplicate]

I'm learning swift, and the course I'm following teaches tableViews. It I have to set the TableViewController t include UITableViewDataSource and UITableViewDelegate. Then, in viewDidLoad, I have to set

tableView.dataSource = self
tableView.delegate = self

in order for the tableView to appear and load data.

Why do I have to do this?

like image 846
Useful_Investigator Avatar asked Aug 28 '16 07:08

Useful_Investigator


People also ask

What is the difference between delegate and datasource?

A data source is like a delegate except that, instead of being delegated control of the user interface, it is delegated control of data. A data source is an outlet held by NSView and UIView objects such as table views and outline views that require a source from which to populate their rows of visible data.

What does delegate self mean?

a delegate is basically the reaction of an event on an object and saying ".delegate=self" means those protocols have been adopted in self ... for eg.. what happens when a row is selected in tableview is told by tableview's delegate method "didSelectRowAtIndexPath" ...

What does Tableview delegate do?

Methods for managing selections, configuring section headers and footers, deleting and reordering cells, and performing other actions in a table view.

What is a delegate in Swift?

In Swift, a delegate is a controller object with a defined interface that can be used to control or modify the behavior of another object. One example is the UIApplicaitonDelegate in an iOS app.


1 Answers

Since you said you're learning Swift just thought of writing an elobrate answer. All the other answers already explains why you need to set the delegate to self or any instance of a class. But still I thought of writing this answer just to give more insight.

Let me explain what UITableViewDelegate & UITableViewDataSource are. Both UITableViewDelegate & UITableViewDataSource are protocols. What is a protocol? You can think protocol as a set of actions.

For example UITableViewDataSource has set of actions/methods like tableView(:numberOfRowsInSection:), tableView( tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) and so on.

What this protocol implies is that if you want to supply your custom data to the tableview, you need to conform to this protocol i.e. implement the non optional methods of the protocol(you can ignore optional methods if any).

Similarly, UITableViewDelegate has set of methods like, tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath), tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) and so on.

UITableViewDelegate protocol implies that you need to conform to the protocol if you want to get notified when user interactions happens at tableview for example when user taps on a cell of tableview.

So now, why are you setting

tableView.dataSource = self
tableView.delegate = self

is because you are implementing the protocols (or conforming to protocols )in your ViewController, TableViewDatasource protocol to supply your own data to the tableview, TableViewDelegate protocol to notify your ViewController class when user interacts with your tableview.

Actually you'll not be setting protocol conformance to self always, you can set it to instance of any class which implements the protocol.

Hope this Helps.

For more reference on protocols you can go through this: Swift 2 Tutorial Part 3: Tuples, Protocols, Delegates, and Table Views

like image 136
iamyogish Avatar answered Oct 10 '22 21:10

iamyogish