Ios 从UICollectionViewCell使用UICollectionView

Ios 从UICollectionViewCell使用UICollectionView,ios,uicollectionview,uicollectionviewcell,Ios,Uicollectionview,Uicollectionviewcell,我有一个自定义UICollectionViewCell,其内容也是一个集合,我想使用UICollectionView显示其内容。这可能吗?我将如何做到这一点?我使自定义UICollectionViewCell也继承了UICollectionViewDataSource,并定义了UICollectionView内部属性,但事情没有按预期进行。有什么想法吗?据我所知,这是不可能的 我尝试在情节提要中执行此操作,但出现错误: “不能将容器视图放置在运行时重复的元素中”这是可能的。通常,您会创建一个管理

我有一个自定义UICollectionViewCell,其内容也是一个集合,我想使用UICollectionView显示其内容。这可能吗?我将如何做到这一点?我使自定义UICollectionViewCell也继承了UICollectionViewDataSource,并定义了UICollectionView内部属性,但事情没有按预期进行。有什么想法吗?

据我所知,这是不可能的

我尝试在情节提要中执行此操作,但出现错误:


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

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


您还没有对当前的尝试或遇到的问题进行任何描述,因此我无法提供更多信息

如BrettThePark所述,无法将容器视图添加到情节提要中的UICollectionVIew原型单元

您可以做的是在序列图像板中创建一个单独的视图控制器,并以编程方式将其添加到您在UICollectionView中使用的UICollectionViewCell子类中的视图中。此视图控制器可以是UICollectionViewController

例如:

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

    [self addSubview:controller.view];
}

下面是一个如何做到这一点的示例。我们有一个带有标准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;
}
结果是自定义视图控制器响应按钮点击。视图在表中出现和消失时,不会调用视图生命周期方法


我这样做是为了证明概念,但对于我正在进行的项目,我看不到将
CellContentViewController
作为视图控制器而不仅仅是自定义视图有任何真正的好处。

将您的
UITableViewController
内容设置为
静态


这是
UICollectionView单元格内
UICollectionView
的一个示例


我并不奇怪你不能在故事板上完成,但这是可能的。谢谢!我最后做了一些接近你建议的事情。在外部集合视图中选择特定单元格时,其视图控制器(集合视图的委托)将推送一个新的视图控制器,该控制器管理内部集合视图。我没有提到的是,两个集合视图之间的区别主要在于布局(网格与水平)和其他一些方面。出于这个原因,我想通过在运行时更改布局来达到同样的效果,但是布局转换不如推一个视图控制器那么平滑。这可以工作,但当ViewDid出现并且不再调用stuff时,可能会给您带来麻烦。doozMen说,您应该将viewController添加为子对象,并在执行soI时使用willMoveToParentView等方法。每个UICollectionViewCell没有视图控制器。因此,您无法将“MyUIViewController”控制器添加到单元格的相应视图控制器,因为没有该控制器。我将collectionView添加到tableViewCell,但仅为第一行加载集合视图。如果我滚动tableview而不是collection view加载第二行的数据等等。但是。。。如果必须为每个单元格添加子视图?(N个单元格)