Using a UICollectionView from a UICollectionViewCe

2019-02-17 13:09发布

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?

6条回答
SAY GOODBYE
2楼-- · 2019-02-17 13:26

This is an example of UICollectionView inside a UICollectionViewCell.

https://github.com/irfanlone/Collection-View-in-a-collection-view-cell

查看更多
乱世女痞
3楼-- · 2019-02-17 13:32

From what I understand this isn't possible.

I tried doing it in storyboard and get the error:

"Container Views cannot be placed in elements that are repeated at runtime"

查看更多
虎瘦雄心在
4楼-- · 2019-02-17 13:36

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.

查看更多
狗以群分
5楼-- · 2019-02-17 13:37

It isn't possible to add a container view to the UICollectionVIew prototype cell in the storyboard, as BrettThePark mentioned.

What you can do is create a separate view controller in the storyboard and add this programatically to the view in the UICollectionViewCell subclass you're using in your UICollectionView. This view controller can off course be a UICollectionViewController.

Example:

-(void)awakeFromNib 
{
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                         bundle: nil];
    UIViewController *controller = [mainStoryboard instantiateViewControllerWithIdentifier:@"MyUIViewController"];

    [self addSubview:controller.view];
}
查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-02-17 13:45

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.

查看更多
可以哭但决不认输i
7楼-- · 2019-02-17 13:50

Make your UITableViewController content as Static.

enter image description here

查看更多
登录 后发表回答