iOS What is the benefit of seperating datasource and delegate

erdinc27

macrumors regular
Original poster
Jul 20, 2011
168
1
hi all. There is a project coded before and i add new features to it. I noticed that in most of the ViewControllers there is a collection view. The coder separated the data source and delegate different classes. Is there any benefit doing this way ?
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
Depends on how large they are. Possibly this can allow code reuse more easily. This is an example of separation of concerns but maybe taken to an extreme. Many developers do recommend separating the data source and delegate into a data provider class that's separate from the view controller. Usually though the data source and delegate are in the same class. Sometimes class extensions are used to separate the two even though they're in the same class and same source file.

In most cases this would be a personal preference aimed at reducing the size of a class and improving understanding of the code.
 

TheWatchfulOne

macrumors 6502
Jun 19, 2009
414
304
In collection and table views, the datasource methods handle the display of data to the screen. For the most part. The delegate methods handle user interaction. Again, for the most part. By separating the datasource and delegate you get the ability to "mix and match" functionality to get the desired results without having to duplicate code.
 

AxoNeuron

macrumors 65816
Apr 22, 2012
1,240
847
The Left Coast
Honestly I have never been a fan of separating the datasource/delegate methods in to separate classes or extensions. I think in the end you end up writing more code and making the project more complicated/convoluted than it really needs to be.

If your code for this table needs to be modular/reusable, then simply make the UITableView/collection view itself a separate class, and put the code in that subclass. That way, all you need to do to reuse this view is to use this subclass
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.