Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top