Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a UICollectionView from a UICollectionViewCell

I have a custom UICollectionViewCell whose content is also a collection and I would like to use UICollectionView to display its content. Is this possible? How would I accomplish this? I made the custom UICollectionViewCell also inheriting from UICollectionViewDataSource and define a UICollectionView internal property but things are not working as expected. Any idea?

like image 407
Glauco Avatar asked Feb 07 '13 01:02

Glauco


2 Answers

Here is an example of how this can be done. We have a storyboard with a standard UITableViewController, a custom UITableViewCell, and a separate freeform custom UIViewController.

// ContainerCell.h
@class CellContentViewController;
@interface ContainerCell : UITableViewCell
@property (nonatomic, strong) CellContentViewController*  contentViewController;
@end

// CellContentViewController.h
@interface CellContentViewController : UIViewController
@property (nonatomic, weak) IBOutlet UILabel*   nameLabel;
@end

// CellContentViewController.m
@interface CellContentViewController ()    
- (IBAction)didTapButton:(UIButton*)sender;    
@end        
@implementation CellContentViewController           
- (void)didTapButton:(UIButton *)sender
{
    NSLog(@"Button for %@!", self.nameLabel.text);
}
@end

// MyTableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* const kCellIdentifier = @"ContainerCell";
    ContainerCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];        
    if (!cell.contentViewController)
    {
        UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];

        CellContentViewController* contentViewController= [storyboard instantiateViewControllerWithIdentifier:@"CellContentViewController"];

        cell.contentViewController = contentViewController;
        [cell.contentView addSubview:cell.contentViewController.view];
    }        
    cell.contentViewController.nameLabel.text = self.names[indexPath.row];                
    return cell;
}

The result is that the custom view controller responds to the button taps. It is not the case that the view lifecycle methods are called as the view appears and disappears from the table.

I did this as a proof of concept, but for the project I'm working on I don't see any real advantage to having CellContentViewController be a view controller instead of just a custom view.

like image 135
Ben Flynn Avatar answered Sep 30 '22 19:09

Ben Flynn


This is possible. Typically you would create a view controller which manages the inner collection view, and add the view of this view controller to the content view of the cell of the outer collection view.

You haven't given any description of you current attempts or problems encountered, so I can't offer much more than this.

like image 33
jrturton Avatar answered Sep 30 '22 17:09

jrturton