What is the benefit of seperating datasource and delegate

Discussion in 'iOS Programming' started by erdinc27, Aug 18, 2017.

  1. erdinc27 macrumors regular

    Joined:
    Jul 20, 2011
    #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 ?
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    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.
     
  3. TheWatchfulOne macrumors 6502

    TheWatchfulOne

    Joined:
    Jun 19, 2009
    #3
    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.
     
  4. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #4
    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
     

Share This Page