ios - UICollectionView 到 UICollectionViewCell

标签 ios uicollectionview uicollectionviewcell

我需要将UICollectionView添加到UICollectionViewCell中。看起来很简单,但我不明白。

我不知道在哪里必须为每个 UICollectionViewCell 初始化 UICollectionView 以及在哪里编程 UICollectionViewDataSourceUICollectionViewDelegate每个 UICollectionView

结果将是一个产品集合(第一个collectionview,其中每个单元格都是一个产品)和每个产品的图像集合,例如图片库(第二个 Collection View ,其中每个单元格都是一个图像) 。它与 Zara 应用相同。

第一个 Collection View 如下所示,并且只有垂直滚动。

enter image description here

当您选择产品时,单元格的大小将更改为全屏大小, Collection View 的垂直滚动将更改为水平滚动。现在垂直滚动用于第二个 Collection View ,即图像库。



第二个 Collection View 如下所示

enter image description here



在 Storyboard中,我有 UICollectionViewControllerUICollectionViewDataSourceUICollectionViewDelegateUICollectionViewDelegateFlowLayout 实现.

ProductsCollectionViewController.h

@interface ProductsCollectionViewController : UICollectionViewController <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout> {
    NSMutableArray *products;
}

ProductsCollectionViewController.m

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return products.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"ProductCell";

    ProductCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    Product *product = [products objectAtIndex:indexPath.row];
    [cell setProduct:product];

    return cell;
}

Product 是一个主要的 NSObject,它包含一些信息以及图像的数组

产品.h

@interface Product : NSObject

@property NSInteger categoryID;
@property NSInteger productID;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *description;
@property (nonatomic, strong) NSArray *images;



我认为我要做的是以编程方式在每个 ProductCollectionViewCell 中使用产品图像的 array 创建新的 UICollectionView,但 ProductCollectionViewCell 无法识别 UICollectionViewDataSourceUICollectionViewDelegate 及其实现。

最佳答案

让我们从一个 mainCollectionView 开始。然后在此集合的每个单元格上创建并初始化一个新的 UICollectionView ,正确的位置是在以下 UICollectionView 委托(delegate)中

func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath)

例如,我在这里初始化 MainCollectionViewCell,然后 MainCollectionViewCell 处理创建新 UICollectionView 的逻辑

guard let collectionViewCell = cell as? MainCollectionViewCell else { return }

    collectionViewCell.delegate = self

    let dataProvider = ChildCollectionViewDataSource()
    dataProvider.data = data[indexPath.row] as NSArray

    let delegate = ChildCollectionViewDelegate()

    collectionViewCell.initializeCollectionViewWithDataSource(dataProvider, delegate: delegate, forRow: indexPath.row)

    collectionViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0

这是 MainCollectionViewCell 上的初始化程序,它创建一个新的 UICollectionView

func initializeCollectionViewWithDataSource<D: protocol<UICollectionViewDataSource>,E: protocol<UICollectionViewDelegate>>(dataSource: D, delegate :E, forRow row: Int) {

    self.collectionViewDataSource = dataSource

    self.collectionViewDelegate = delegate

    let flowLayout = UICollectionViewFlowLayout()
    flowLayout.scrollDirection = .Horizontal

    let collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: flowLayout)
    collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseChildCollectionViewCellIdentifier)
    collectionView.backgroundColor = UIColor.whiteColor()
    collectionView.dataSource = self.collectionViewDataSource
    collectionView.delegate = self.collectionViewDelegate
    collectionView.tag = row

    self.addSubview(collectionView)

    self.collectionView = collectionView

    collectionView.reloadData()
}

希望有帮助!!

我为此做了一个示例并放在 github 上。它演示了在 UICollectionViewCell 中使用 UICollectionView

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

关于ios - UICollectionView 到 UICollectionViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21316330/

相关文章:

ios - 在 Objective C 中将 Hex NSData 转换为 int

ios 在 Masonry 中设置 uiscrollview 的 contentsize

ios - Swift 4.2 使用 UICollectionViewCell 添加/删除按钮

ios - 未调用 UICollectionView cellForItemAtIndexPath 方法

ios - 在 collectionViewCell 中填充 tableView

iphone - 如何绘制弹出 View 而不是在 iPhone 上使用图像?

ios - Apple PushNotification : Need Server Side functionality Guide

ios - UICollectionView 未完全显示单元格。最后一个单元格没有完全显示

ios - 来自 UICollectionViewCell 的 UIAlertController

ios - UICollectionView - 多个不同的单元格