使用UICollectionViewCell中的UICollectionView

Gla*_*uco 5 ios uicollectionview uicollectionviewcell

我有一个自定义的UICollectionViewCell,其内容也是一个集合,我想使用UICollectionView来显示其内容.这可能吗?我怎么做到这一点?我使自定义UICollectionViewCell也继承自UICollectionViewDataSource并定义了一个UICollectionView内部属性,但事情没有按预期工作.任何的想法?

Ben*_*ynn 7

这是一个如何做到这一点的例子.我们有一个带有标准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;
}
Run Code Online (Sandbox Code Playgroud)

结果是自定义视图控制器响应按钮点击.当视图出现并从表中消失时,不会调用视图生命周期方法.

我这样做是为了概念验证,但对于我正在研究的项目,我认为CellContentViewController成为一个视图控制器而不仅仅是一个自定义视图并没有任何真正的优势.


jrt*_*ton 4

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

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