does a tableView have to have a delegate?

I'm trying to discover the differences between dataSources and delegates. I'm working with a tableView in a macOS cocoa app. I'm trying to keep things as simple as possible, so I'm trying just using the NSTableViewDataSource protocol and reloadData to update the table. I've almost got it working, but the one example I'm drawing from uses a delegate to update the tableView. So am I doomed to failure without using a delegate, or can a tableView update without the aid of a delegate?


Thanks!

Accepted Reply

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…

Replies

I'm trying to discover the differences between dataSources and delegates.


DataDource is for functions dealing with populating the table


func tableView(UITableView, cellForRowAt: IndexPath) -> UITableViewCell

Asks the data source for a cell to insert in a particular location of the table view.

Required.


func numberOfSections(in: UITableView) -> Int

Asks the data source to return the number of sections in the table view.


func tableView(UITableView, numberOfRowsInSection: Int) -> Int

Tells the data source to return the number of rows in a given section of a table view.

Required.


func tableView(UITableView, titleForHeaderInSection: Int) -> String?

Asks the data source for the title of the header of the specified section of the table view.


func tableView(UITableView, titleForFooterInSection: Int) -> String?

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


func tableView(UITableView, willSelectRowAt: IndexPath) -> IndexPath?

Tells the delegate that a specified row is about to be selected.


func tableView(UITableView, didSelectRowAt: IndexPath)

Tells the delegate that the specified row is now selected.


func tableView(UITableView, willDeselectRowAt: IndexPath) -> IndexPath?

Tells the delegate that a specified row is about to be deselected.


func tableView(UITableView, didDeselectRowAt: IndexPath)

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…