使用UICollectionViewCell中的UICollectionView

我有一个自定义的UICollectionViewCell,其内容也是一个集合,我想使用UICollectionView来显示其内容。 这可能吗? 我将如何做到这一点? 我做了自定义UICollectionViewCell也inheritance自UICollectionViewDataSource并定义一个UICollectionView内部属性,但事情不能按预期的方式工作。 任何想法?

这个有可能。 通常,您将创build一个视图控制器来pipe理内部集合视图,并将此视图控制器的视图添加到外部集合视图的单元格的内容视图中。

你没有给你任何描述你目前的尝试或遇到的问题,所以我不能提供比这更多。

这是一个如何做到这一点的例子。 我们有一个标准的UITableViewController,一个自定义的UITableViewCell和一个单独的自由格式自定义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; } 

结果是自定义视图控制器响应button敲击。 视图生命周期方法不是在视图出现并从表中消失的情况下调用的。

我做了这个概念的certificate,但对于我正在进行的项目,我没有看到任何真正的好处,让CellContentViewController是一个视图控制器,而不是一个自定义视图。

正如BrettThePark所说,无法在故事板中的UICollectionVIew原型单元格中添加容器视图。

你可以做的是在故事板中创build一个单独的视图控制器,并将其编程添加到您在UICollectionView中使用的UICollectionViewCell子类中的视图。 这个视图控制器可以是一个UICollectionViewController。

例:

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

这是UICollectionView一个UICollectionViewCell

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

从我所了解的这是不可能的。

我试图在故事板上做,并得到错误:

“容器视图不能放置在运行时重复的元素”

使您的UITableViewController内容为Static

在这里输入图像说明