UICollectionView with full page cells VS UIScrollView with child view controllers

Multi tool use


UICollectionView with full page cells VS UIScrollView with child view controllers
I'm trying to create a ViewController which would have swipe-able (android like tabs) pages. These pages themselves will have scrollviews(vertical) inside them and multiple views which would be added dynamically depending on type of response (different network calls for each page). I can't use a PageViewController as I want the pages to take up only half the screen.
Issues with CollectionView -
Issues with ScrollView -
PS - data in each page would be 4-10 stackviews each containing 2-10 images/labels OR just one collectionview
PSS - Total number of tabs wouldn't exceed 10, minimum would be 1
@Nickolans Keeping track of data isn't the difficult part, each page would create different type of views depending on the different types of response they get, so if I use a collectionview then I would have to remove all subviews in scrollview and create them again with all the data I stored.
– Akash Popat
yesterday
Use PageViewController ?. For your child view controller you can think different methods to handle memory issues like load thumb image or you can use collectionview / tableview instead of stackview there
– Prashant Tukadiya
yesterday
@PrashantTukadiya Can't use a PageViewController because I don't want the pages to take the whole screen(check question). Can't use collectionview inside the pages because the views in each block(stack) are very different from each other. They may be similar across pages but not in the same page.
– Akash Popat
yesterday
I think your stated issue with CollectionView is not an issue but exactly how you should approach it. Manage your data(source) well and draw your UI based on the data you have. The concept of cell reuse is precisely meant for those cases. Developers should not use cells (or other UI elements) as their data storage.
– Au Ris
20 hours ago
1 Answer
1
I'd implemented it with collectionView cause it should be really more resource effective. But then we need to cache states of view controllers. Here is the example
Let's say you have controller A
which contains collectionView with cell with your child controllers. Then in cell for row
A
....
var childrenVC: [Int: UIViewController] = [:]
....
// cell for row
let cell: ChildControllerCell = collectionView.dequeueReusableCell(for: indexPath)
if let childController = childrenVC[indexPath.row] {
cell.contentView.addSubview(childController.view)
childController.view.frame = cell.contentView.frame
} else {
let childViewController = ChildViewController()
addChildViewController(childViewController)
childViewController.didMove(toParentViewController: self)
cell.contentView.addSubview(childController.view)
childController.view.frame = cell.contentView.frame
childrenVC[indexPath.row] = childViewController
cell.childVC = childViewController
}
return cell
....
class ChildControllerCell: UICollectionViewCell {
var childVC: UIViewController?
override func prepareForReuse() {
super.prepareForReuse()
if !contentView.subviews.isEmpty {
childVC?.willMove(toParentViewController: nil)
childVC?.view.removeFromSuperview()
childVC?.removeFromParentViewController()
}
}
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Have you thought about using CoreData to keep track of the data you'd like?
– Nickolans
yesterday