OdieO
OdieO

Reputation: 7004

UITableView && UICollectionView in one controller?

I'm trying to implement iPhone and iPad versions gracefully. They're essentially the same controller, but one the iPhone presents in a single column tableView and the iPad in a multi-column collectionView.

Here are the options I thought of:

  1. One ViewController that asks for a different DataSource and different Delegate for tableView vs collectionView. I haven't thought this option all the way through. It does seem like it might get overly complicated.

  2. One baseViewController and then 2 subclasses, one with a tableView and one with a collectionView. Similar to option #1 but using subclasses. Option #1 is cleaner, but this would be easier to implement.

  3. Just use a collectionView for both, but use different UICollectionViewFlowLayouts. This was the last option I thought of and seems like by far the best option, but I'm wondering if there's a reason why #1 or #2 might be better.

Upvotes: 3

Views: 784

Answers (1)

OdieO
OdieO

Reputation: 7004

So I ended up going with 1 UICollectionView, and IMO it works great.

I had to write conditionals for only 3 parts:

  1. Returning different collectionViewFlowLayouts
  2. Returning different sizes for collectionView:layout:sizeForItemAtIndexPath:
  3. Different layouts for layoutSubviews of the UICollectionViewCell

Being able to use the same UICollectionViewCell was a really big plus since I am implementing a number of buttons within the cell, and I didn't have to replicate delegates for exactly similar logic in a UICollectionViewCell AND a UITableViewCell.

Upvotes: 1

Related Questions