Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Child view controller in UICollectionViewCell

Suppose I want to achieve Pinterest's pin page, like this one:

pinterest

This is my approach:

  1. make a UICollectionViewController, pin's page is a UICollectionViewCell
  2. cell is make of two components: pin info child vc && waterfall child vc

Then comes the problem: How can I reuse child view controller?

Some pseudo code:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    Pin *pin = self.dataSource[indexPath.row];
    // i have to create a new childVC, based on different indexPath.
    UITableViewController *pinInfoViewController = [[pinInfoViewController alloc] initWithPin:pin];
    [cell updatePinViewWithPin:pin];
    [self addChildViewController:pinInfoViewController];

    // Add waterfall view controller
}

Every time this method is called, a new child view controller will be created, is it ok, or how to improve it?

like image 589
limboy Avatar asked Mar 17 '14 04:03

limboy


People also ask

What is child view controller in Swift?

Child view controllers are especially useful for UI functionality that we wish to reuse across a project. For example, we might want to display a loading view as we're loading the content for each screen — and that can easily be implemented using a child view controller, that can then simply be added when needed.

How do I embed a view controller?

Just drag a container view out into your main view controller and use the embed segue from it to your embedded view controller. It will properly set up all the view controller hierarchy for you.

What is view controllers in IOS?

View controllers are the foundation of your app's internal structure. Every app has at least one view controller, and most apps have several. Each view controller manages a portion of your app's user interface as well as the interactions between that interface and the underlying data.

What does view controller do?

A view controller manages a single root view, which may itself contain any number of subviews. User interactions with that view hierarchy are handled by your view controller, which coordinates with other objects of your app as needed. Every app has at least one view controller whose content fills the main window.


2 Answers

I've run across a similar situation recently and struggled choosing between various solutions like those detailed in UIViewController within UICollectionView. It appears that there's an open source project that encapsulates this pattern available now: https://github.com/zats/Voltron. If your problem might be best solved by having a UICollectionView of UIViewControllers then it's easier to accomplish than trying to roll your own.

like image 89
michaelcameron Avatar answered Oct 23 '22 13:10

michaelcameron


The way I would approach this is to subclass UICollectionViewCell and add the UI components you need to it as iVars. When you need to update the UI with new data you would grab the cell object like you are now in the pseudo code and then call a method you declare, could be updateCellWithModel: and pass it the model that is held in the datasource. In this method you would do some simple checks for if the UI elements are created or not and create them if needed, its always a good idea to have sanity checks in these types of methods but the elements should be created in the init method and will always be there.

EDIT: I believe I answered your question but it still confuses me, please add more info so I can edit my answer if needed.

like image 1
ApperleyA Avatar answered Oct 23 '22 13:10

ApperleyA