Tariq
Tariq

Reputation: 9979

Pagination engine architecture - iOS

What would be best way to create a pagination engine architecture ?

In my previous implementation I'd

- UIViewController
   - property UITableView
   - with two sections in tableView. One for normal display and other 
   - for PaginationCell 
   - and rest of the business logic how to enable and hide indicator View

We thought this is a too much of duplicate code for every feature and decided to remove duplicate code.

And we wrote a category on UIViewController to handle data pagination. Now we handle all calculation inside the category using scrollViewDidScroll and add indicator footer view using objc_setAssociatedObject.

I believe Associated objects should be seen as a method of last resort, rather than a solution in search of a problem (and really, categories themselves really shouldn’t be at the top of the toolchain to begin with).

So my question is that - Is this new design a best solution for the problem. Or would you recommend some better approach ?

Upvotes: 0

Views: 118

Answers (1)

Matteo Gobbi
Matteo Gobbi

Reputation: 17707

I don't get exactly what is the duplicated code you don't want, but I don't think at all that using a category is a good approach for this problem.

What I can suggest is to have a good architecture with separated responsibilities. This will lead you to the solution.

If can help you can read my article Anatomy of a TableView(Controller) Architecture and the one specific on Interaction Objects. You can imagine Interaction Objects like boxes with all the information the cell needs, like:

  • What should I display?
  • What kind of cell should I use?
  • What I have to do if the use select this cell (target, selector, ..)? etc.

In general, following the approach described you should not feel stuck in front the most of the situations.

;)

For pagination, I would create a manager which is a dependence of the table view controller. The idea is:

  1. User does something on the tableView;
  2. The tableView's delegate (through interaction objects, delegate or blocks) communicates to the view controller the event;
  3. The view controller, ask to the pagination manager the new parameters;
  4. The view controller pass to the tableView's datasource the new parameters and reload it;

Upvotes: 1

Related Questions