Child view controller in UICollectionViewCell
Asked Answered
C

3

7

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?

Contentment answered 17/3, 2014 at 4:6 Comment(0)
T
1

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.

Tidal answered 17/3, 2014 at 4:58 Comment(2)
gotcha, but how to deal button (like 'Added by XXX') event? if use child vc, button event can be handled there. but if added as a cell's subView property, it should be handled in the UICollectionViewController, right? so a fat controller is born.Contentment
handle view's touch event is part of a controller's duty, so it's ok.Contentment
G
2

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.

Galer answered 12/12, 2014 at 2:44 Comment(0)
T
1

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.

Tidal answered 17/3, 2014 at 4:58 Comment(2)
gotcha, but how to deal button (like 'Added by XXX') event? if use child vc, button event can be handled there. but if added as a cell's subView property, it should be handled in the UICollectionViewController, right? so a fat controller is born.Contentment
handle view's touch event is part of a controller's duty, so it's ok.Contentment
F
0

I don't think they're using a collection view cell with an embedded table view here. It looks like a normal view controller that is dedicated to a pin. The data pulled from the server for said pin probably contains information about the name of the pin, a link to the pin, maybe an image, all the people who have added the pin, who the original pinner was, likes, favorites, etc., and the view controller parses that data accordingly. The controller uses that data to update the UI and that's that.

I do see a table view and collection view that Pinterest could have used for the implementation of this view controller, but it doesn't look like they embedded all of this data into a collection view cell.

Fid answered 24/9, 2015 at 21:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.