I'm trying to discover the differences between dataSources and delegates.
- DataDource is for functions dealing with populating the table
Asks the data source for a cell to insert in a particular location of the table view.Required.
Asks the data source to return the number of sections in the table view.
Tells the data source to return the number of rows in a given section of a table view.Required.
Asks the data source for the title of the header of the specified section of the table view.
Asks the data source for the title of the footer of the specified section of the table view.
Note that 2 of those func are required.
- Delegate are for the behavior of the table, such as
Tells the delegate that a specified row is about to be selected.
Tells the delegate that the specified row is now selected.
Tells the delegate that a specified row is about to be deselected.
Tells the delegate that the specified row is now deselected.
Note that none is required, that's why you can (but should not) omit.
can a tableView update without the aid of a delegate?
If you don't use any delegate func, you don't need. But it is a bit risky.
I noticed similar question with pickers: if I omitted the datasource reterence, things kept working.
I filed a DTS and got the following answer
Note: filed a DTS (695575082) and got the advice to file this bug report.:
" This is as a result of iOS moving away from informal to formal protocol definitions.
The runtime checks if you conform and implement those methods and they get called.
You should definitely set the picker view’s dataSource to your view controller.
If not, when these are called:
func numberOfComponents(in pickerView: UIPickerView) -> Int
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
… “pickerView” will be nil. Often times you want that to be valid value so to be sure you are targeting the right picker view (in the case of multiple picker views at once).
I consider this a bug in that you “should” set the dataSource. "
So, conclusion is : you SHOULD set a delegate and dataSource.
This can be done in IB, so it is really simple.
Great answer! Thank you! Can I ask a follow up question? Right now I have both a ViewController Class and an AppDelegate class. My tendency would be to put all tableView methods (both dataSource and delegate related) in the ViewController class, and leave the AppDelegate class for bigger app-related delegation things. Does that sound like good practice?
My tendency would be to put all tableView methods (both dataSource and delegate related) in the ViewController class and leave the AppDelegate class for bigger app-related delegation things
Yes, that's what you have to do. Always declare objects where they really belong to ; here the tableView should be in the View Controller.
One more advice.
When you define the view controller, give it an explicit name, such as
class WelcomeViewController: UIViewController
and not keep the default proposal of
class ViewController: UIViewController
That will help you when you have multiple View Controllers.
And don't forget to close the thread…