Ios UICollectionView到UICollectionViewCell
我需要将Ios UICollectionView到UICollectionViewCell,ios,uicollectionview,uicollectionviewcell,Ios,Uicollectionview,Uicollectionviewcell,我需要将UICollectionView添加到UICollectionViewCell中。这似乎很容易,但我不明白 我不知道在哪里必须为每个UICollectionViewCell初始化UICollectionView,以及在哪里编程UICollectionViewDataSource和UICollectionViewDelegate 结果将是产品集合(第一个collectionview,其中每个单元格都是产品)和每个产品的图像集合,如图像库(第二个集合视图,其中每个单元格都是图像)。它与Zar
UICollectionView
添加到UICollectionViewCell
中。这似乎很容易,但我不明白
我不知道在哪里必须为每个UICollectionViewCell
初始化UICollectionView
,以及在哪里编程UICollectionViewDataSource
和UICollectionViewDelegate
结果将是产品集合(第一个collectionview
,其中每个单元格都是产品)和每个产品的图像集合,如图像库(第二个集合视图,其中每个单元格都是图像)。它与Zara应用程序相同
第一个集合视图看起来像这样,只有垂直滚动
选择产品时,单元格的大小将更改为全屏大小,集合视图将垂直滚动更改为水平滚动。现在,垂直滚动用于第二个集合视图,即图像库
第二个集合视图如下所示
在故事板中,我有
UICollectionViewController
和UICollectionViewDataSource
,UICollectionViewDelegate
和UICollectionViewDelegateFlowLayout
实现
ProductsCollectionViewController.h
@interface ProductsCollectionViewController : UICollectionViewController <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout> {
NSMutableArray *products;
}
@interface Product : NSObject
@property NSInteger categoryID;
@property NSInteger productID;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *description;
@property (nonatomic, strong) NSArray *images;
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;
}
产品是一个包含一些信息的主NSObject
,也是一个图像的数组
产品.h
@interface ProductsCollectionViewController : UICollectionViewController <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout> {
NSMutableArray *products;
}
@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
中使用产品的图像数组以编程方式创建新的UICollectionView
,但是ProductCollectionViewCell
不识别UICollectionViewDataSource
和UICollectionViewDelegate
实现。让我们从拥有mainCollectionView
开始。然后在此集合的每个单元格上创建并初始化一个新的UICollectionView
,正确的位置是在下面的UICollectionView委托中
func collectionView(collectionView:UICollectionView,willDisplayCell:UICollectionViewCell,forItemAtIndexPath indexPath:NSIndexPath)
e、 g我在此处初始化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
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()
}
这是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
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()
}
func initializeCollectionViewWithDataSource(数据源:D,委托:E,forRow:Int){
self.collectionViewDataSource=数据源
self.collectionViewDelegate=委托
让flowLayout=UICollectionViewFlowLayout()
flowLayout.scrollDirection=.Horizontal
让collectionView=UICollectionView(帧:self.bounds,collectionViewLayout:flowLayout)
registerClass(UICollectionViewCell.self,forCellWithReuseIdentifier:reuseChildCollectionViewCellIdentifier)
collectionView.backgroundColor=UIColor.whiteColor()
collectionView.dataSource=self.collectionViewDataSource
collectionView.delegate=self.collectionViewDelegate
collectionView.tag=行
self.addSubview(collectionView)
self.collectionView=collectionView
collectionView.reloadData()
}
希望有帮助
我为此做了一个示例,并在github上进行了介绍。它演示了在UICollectionViewCell
中使用UICollectionView
使用故事板也是可能的
受到启发
主要的技巧是“willDisplayCell”方法。只需确保使用了正确的UICollectionViewCell类
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(PNCollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
if ([cell isKindOfClass:[PNCollectionViewCell class]]) {
[cell setCollectionViewDataSourceDelegate:self indexPath:indexPath];
NSInteger index = cell.collectionView.indexPath.row;
CGFloat horizontalOffset = [self.contentOffsetDictionary[[@(index) stringValue]] floatValue];
[cell.collectionView setContentOffset:CGPointMake(horizontalOffset, 0)];
}
}
有关更多详细信息,请参见我的示例:
嗯。。。看来你把事情弄得太复杂了。你想要实现什么?你能澄清一下你想要实现什么吗?另外,您是在使用故事板还是以编程方式设置所有内容?我需要这样做,但要使用UICollectionViewController而不是UITableViewController。如果您需要,我可以放一些代码来澄清。“但是ProductCollectionViewCell不识别UICollectionViewDataSource和UICollectionViewDelegate及其实现”——您能详细说明一下您的意思吗?您的方法应该有效。第二个视图看起来是页面视图控制器,而不是集合视图控制器