我需要将UICollectionView
添加到UICollectionViewCell
中。看起来很简单,但我不明白。
我不知道在哪里必须为每个 UICollectionViewCell
初始化 UICollectionView
以及在哪里编程 UICollectionViewDataSource
和 UICollectionViewDelegate每个
。UICollectionView
的
结果将是一个产品集合(第一个collectionview
,其中每个单元格都是一个产品)和每个产品的图像集合,例如图片库(第二个 Collection View ,其中每个单元格都是一个图像) 。它与 Zara 应用相同。
第一个 Collection View 如下所示,并且只有垂直滚动。
当您选择产品时,单元格的大小将更改为全屏大小, Collection View 的垂直滚动将更改为水平滚动。现在垂直滚动用于第二个 Collection View ,即图像库。
第二个 Collection View 如下所示
在 Storyboard中,我有 UICollectionViewController
和 UICollectionViewDataSource
、UICollectionViewDelegate
和 UICollectionViewDelegateFlowLayout
实现.
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
无法识别 UICollectionViewDataSource
和 UICollectionViewDelegate
及其实现。
最佳答案
让我们从一个 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/