Search code examples
ioscode-structure

How to structure my iOS code?


I have quite a complex setup, and am looking for advice in how to structure my code. What I'm trying to do is something similar to twitter when you press one of a users images and can swipe through them. This is what I have at the moment:

  • A UIViewController subclass that gathers data from a server, and passes it to a UITableViewController subclass through a variable called theData
  • This UITableViewController subclass also gathers more data as the users scrolls to the bottom of the table view and appends it to theData (i.e. uses pagination). This UITableViewController subclass is reused throughout the app so that data from a different source can be passed to it.
  • When a user clicks on a UITableViewCell, a UIWindow is created which has a UIPageViewController inside it. The window uses the UITableViewController as a delegate to access theData, and passes the theData from the delegate to the UIPageViewController in a variable called pageData
  • Each page of the UIPageViewController is a UIViewController that loads images from the pageData

The next stage is to do pagination as the user swipes to the end of the pageData in the UIPageViewController. However I don't think my code is structured well enough to reuse the pagination code from the UITableViewController. Please could someone give me advice on how to improve this code structure?

Thank you

EDIT

The UIViewController in the first bullet point does more than just downloading data (it has other views within it).

I use a UIWindow to encapsulate the UIPageViewController because I want it to expand from the frame of the UITableViewCell and "take over" the full screen - covering the status bar.


Solution

  • My advice would be to create simple service which you can use for communication with the server. You don't need View controller for that.

    Further more you can create static methods that will give you theData without passing it all over the application.

    Regarding the swipe, you can take 3 users in an array, and corresponding to scroll, load additional one, and release unnecessary one. That would help you with memory usage ( if users swipes through 50 or more users ).

    Instead of UIWindow, create simply segue from UITableViewController to DetailView, or in your situation, UIPageViewController.