0

This has bugged me for a long time. There are two delegate protocols defined for UITableView: the actual delegate and the data source. However, given the way the methods are distributed between the two, I'm yet to see a situation when it is practical to implement the protocols in two separate objects. Could anyone give me an example when it actually makes sense to do so?

konrad
  • 1,664
  • 2
  • 17
  • 36

2 Answers2

0

Short answer: Yes it does.

Long answer:

They are split in 2 different protocols, because they serve different purposes.

The delegate is responsible for managing aspects of the table itself like selection, reordering and deletion of rows, ... UITableViewDelegate Protocol Reference

In contrast to that UITableViewDatasource is responsible for adapting your data model so that it fits the needs of the table. It provides the cells for the table, tells the table how many sections there are, how many rows per section there are, ... UITableViewDatasource Protocol Reference

Depending on your data and what you want to do with the table, that code can become arbitrarily large. If it's getting too large, you can split it up for better navigation and overview. That would be one case I can think of where this distinction can improve code quality.

Stefan Jager
  • 141
  • 1
  • 10
0

IMO you don't get any benefits by doing so, as the datasource just provides methods to interact with data, same as the delegate. If your store is big and clumsy, separating the datasource won't solve the issue.

No added value

You can get some unpredictable behavior when forcing the separation:

UITableView issue when using separate delegate/dataSource

How to avoid big and clumsy UITableViewController on iOS?


It depends on the specifics of the app

On the other hand, depending on the app design it may be required to separate the datasource from the delegate:

Lighter View Controllers

UITableView delegate and datasource in a separate class after parsing

Community
  • 1
  • 1
carlodurso
  • 2,886
  • 4
  • 24
  • 37