In my app I have a home page which has a Card/Grid Layout with multiple section. So UICollectionView
is the obvious choice here. But these cards vary a lot in design, loading data and their functionality too. So putting all those in one single controller would be cumbersome, complex and hard to maintain. So we thought we need to separate these out into their own UIViewControllers
. Each handles a type of card and calculates its contentSize to be shown inside a card. Our main home page controller is only responsible for putting those viewcontroller
views inside the UICollectionView
cells.
To summerize,
My question is this, is this design choice right? Or what approach would you chose to implement such a screen? Since we create a lot of viewControllers (one for every cell), does this affect memory or performance? And If I want to cache only some viewControllers or load them as the user scrolls or when its time to add it to the collectionView cell, how do I go about it as calculating the size of that cell depends upon the data and the views while laying out the CollectionView layout.
I have a UICollectionView inside a UIViewController. The collectionView holds an array of 8 identifiers of view controllers from the storyboard. Each view controller in the array inherited from the UIViewController that holds the collectionView.
Like a table view, a collection view is a UIScrollView subclass. UICollectionViewCell: This is similar to a UITableViewCell in a table view. These cells make up the view’s content and are subviews to the collection view. You can create cells programmatically or inside Interface Builder.
A single data item when that item is within the collection view’s visible bounds. You can use UICollectionViewCell as-is or subclass it to add additional properties and methods. The layout and presentation of cells is managed by the collection view and its corresponding layout object.
UICollectionView contains several key components, as you can see below: UICollectionView: The main view where the content is displayed, similar to a UITableView. Like a table view, a collection view is a UIScrollView subclass. UICollectionViewCell: This is similar to a UITableViewCell in a table view.
I think your approach is valid and from a theoretical standpoint it makes sense to separate a view from its data. But, in this case I would prefer to have the UICollectionViewCell subclass implement its own data, basically acting as a viewController. This will simplify your code a bit since you don't need the added viewControllers and the code that comes with them; instead you can simply add your tableView to the cell's contentView. This seems more natural to me, as then you can have HomeViewController's collectionView automatically handle calling its delegates when cells are moved offscreen and on, and during the loading process. You won't need to send messages to cells about who they are or when they should load, this will all be taken care of for you automatically. Also, I really don't think having the view lifecycle methods on each cell is valuable, and is less intuitive than having a cell load and refresh as part of a collectionView. Either way works, good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With