3

I'm trying to develop a universal iOS app and ideally I'm trying to get as much code re-use as possible.

I'm using storyboards so the UI is segmented into scenes.

In the iPhone storyboard the particular scene in question is best suited to a table view. In the iPad storyboard, the equivalent scene in the storyboard would ideally implement a UICollectionView and so render the content in a grid.

Both views should implement a pull-to-refresh control which in one case is going to call a [UITableView reloadData] and in the other a [UICollectionView reloadData].

Anybody any advice on how to get the maximum amount of code re-use, ie. what does the controller class look like?

1 option would be to have 1 monolithic class that conforms to both the UITableView protocols and the UICollectionView protocols but this doesn't feel right.

Another option would be to have a base class and then subclass it with specialisations for iPhone and iPad. Whilst this feels cleaner, I'm still thinking there might be a better way. In my first attempt at this it felt like there was more code in the specialisation classes than there was in the base class.

The third plan I've considered is simply to use a UICollectionView in both apps therefore eliminating the complexity of the controller. Hopefully I could force the layout of the collection view to be a grid on the iPad whilst tell it to mimic a table view on the iPhone.

Whilst on the face of it this sounds easy I'm struggling to make a UICollectionView work exactly like a table view would do normally.

Any advice guys?

Many thanks,

CA.

Chris A
  • 116
  • 1
  • 7

2 Answers2

2

If you are not using some of the specific UITableView features[1], making UICollectionView look exactly like a table is trivial. I think I would go for using UICollectionView as it will allow more flexibility in the future and you will have to use it anyways.

Community
  • 1
  • 1
Jaroslav
  • 1,389
  • 14
  • 27
0

When you are talking about code reuse you probably mean the DRY principle. It just says you should not write the same code or the same information twice, but I don't think this is your main issue here.

I would use a UITableViewController on the iPhone and a UICollectionViewController on the iPad. I think this is the cleanest and easiest way to go. What exactly do you hope to reuse among both controllers? The both require two different delegate protocols, so most of the methods will be specific to one platform. You will need two NSFetchedResultsController if you're using Core Data, but reusing the fetched results controller code only makes sense if you need the same data on both platforms.

If you create a new universal project in Xcode you will get a basic project setup that looks different on iPhone and iPad. Try to understand it (there are some details to pay attention to) before you start programming, I think it will answer at least some of your questions.

Paul
  • 999
  • 1
  • 12
  • 29
  • Thanks for taking the time to answer. Perhaps I am being too 'pure' in what I'm trying to achieve. In terms of what I wanted to avoid duplicating, both controllers will have a pull to refresh control - so there's a handler for that. The handler will essentially call a method on AFNetworking to return exactly the same data in both cases. It will also then call reload on either the collection view or table view. – Chris A Aug 06 '13 at 16:20
  • Sorry, my original comment got cut short, I was curious to know if I'd overlooked something obvious. In WPF the controls are based on a hierarchy so in this situation you could probably have found a common base class or interface and coded against that. I know that the delegates are different but they are so closely related it just felt wrong. Anyway, it looks like I haven't overlooked anything and there are definitely bigger issues to worry about! Thanks again. C. – Chris A Aug 06 '13 at 16:29