2
votes

I have a table view where each row contains a collectionView. Each collectionView is only one row tall and scrolls horizontally. So, if you can imagine, I have several horizontally scrolling carousels stacked on top of each other.

I've seen several tutorials for this type of layout and they all have a single UIViewController which is the dataSource and delegate for both the UITableView and all UICollectionViews. This allows that viewController to reuse tableView cells and the collectionViews inside them.

Because of some other code reuse considerations, I would like to structure my code differently. I would like to have a UITableViewController which manages the table and then have UICollectionViewControllers which manage each collection view. This does work but because UICollectionViewController contains its own UICollectionView, I can not reuse collectionViews as the user scrolls through the the table exposing different carousels.

So finally my question, can you inject or replace the collectionView used by a UICollectionViewController so that I can reuse views across multiple View Controllers.

2

2 Answers

1
votes

Well, i guess i've done what you're asking for. You want something like AppStore does have.

Let me describe you what i have done, i have created a custom tableViewCell which has a collectionView as its property, from my tableViewController i send my custom tableViewCell. now the important part is, what about collectionView? so i thought that what if we set collectionView's dataSource and delegate to tableViewCell? it'll handle everything individually. i have created some delegate methods of my tableViewCell to handle didSelect and other events of collectionView. this way you dont need a collectionViewController only a tableViewController and a custom tableViewCell will do a job.

Please let me know if its confusing you or need any sample code(i'll add it later if you want)

Edit

forgot to mention that you can't inject a collectionView of a collectionViewController to another collectionView

Also to reuse your code you can create a class lets call CollectionViewHandler and add datasource and delegate method to that class and assign an instance of that class to individual collectionView for datasource and delegate. Here using collectionViewController is not an option for your case.

0
votes

No, I don't think you can. Views can only belong to a single superview.

For that matter, if you have a single view controller that manages multiple collection views, each collection view has it's own queue of cells for reuse. You send the dequeueReusableCell(withReuseIdentifier:for:) to a specific collection view, and each one maintains a separate reuse queue.

Why do you think you need to share all of your cells, views across all of your collection views? The point of cell reuse it to avoid "churn" when scrolling. With cell reuse, you reach a "high water mark" of the max number of cells on-screen at a time, and when you scroll, the ones that scroll off-screen get reused to display new data at other indexPath positions in your collection.

It seems to me that you're looking to solve a problem that doesn't exist.

You said:

I've seen several tutorials for this type of layout and they all have a single UIViewController which is the dataSource and delegate for both the UITableView and all UICollectionViews. This allows that viewController to reuse tableView cells and the collectionViews inside them.

That's not right. Each collection view has a separate queue of cells to recycle. Cells won't cross from one collection view to another.