Using a UICollectionView from a UICollectionViewCell

后端 未结 6 1590
感情败类
感情败类 2021-01-13 06:38

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 accomp

相关标签:
6条回答
  • 2021-01-13 06:48

    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"

    0 讨论(0)
  • 2021-01-13 06:50

    This is an example of UICollectionView inside a UICollectionViewCell.

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

    0 讨论(0)
  • 2021-01-13 06:54

    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.

    0 讨论(0)
  • 2021-01-13 06:55

    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.

    0 讨论(0)
  • 2021-01-13 07:02

    Make your UITableViewController content as Static.

    enter image description here

    0 讨论(0)
  • 2021-01-13 07:13

    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];
    }
    
    0 讨论(0)
提交回复
热议问题