我想在同一个Viewcontroller中保留多个uicollectionview控件.pl建议我代码或任何链接
答案 0 :(得分:1)
您需要设置两个集合视图的“tag”值,然后使用以下代码检查标记值:
if (collectionView.tag == 0) {
// collection view 1
}
else if (collectionView.tag == 1) {
// collection view 2
}
您也可以在界面构建器或代码中设置标记值。使用setTag:
方法。
答案 1 :(得分:1)
这只是一个想法
像这样初始化:
CGRect mainFrame = self.view.frame;
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
collectionView1=[[UICollectionView alloc] initWithFrame:CGRectMake(0, mainFrame.origin.y , 320, 250) collectionViewLayout:layout];
[collectionView1 setDataSource:self];
[collectionView1 setDelegate:self];
[collectionView1 registerClass:[Cell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[collectionView1 setBackgroundColor:[UIColor redColor]];
[self.view addSubview:collectionView1];
[collectionView1 reloadData];
UICollectionViewFlowLayout *layout2=[[UICollectionViewFlowLayout alloc] init];
collectionview2 = [[UICollectionView alloc] initWithFrame:CGRectMake(0, mainFrame.origin.y + 270, 320, mainFrame.size.height-280) collectionViewLayout:layout2];
[collectionview2 setDataSource:self];
[collectionview2 setDelegate:self];
[collectionview2 registerClass:[Cell2 class] forCellWithReuseIdentifier:@"destCellIdetifier"];
[collectionview2 setBackgroundColor:[UIColor lightGrayColor]];
[self.view collectionview2];
[collectionview2 reloadData];
写下数据源和委托如下
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if (collectionView == collectionView1) {
return 18;
}
else
return 8;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Cell *cell1 = nil;
if ([collectionView isEqual:collectionView1]) {
cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell1.label.text = [NSString stringWithFormat:@"%d",indexPath.item];
}
else {
Cell2 *cell2 = [collectionView dequeueReusableCellWithReuseIdentifier:@"destCellIdetifier" forIndexPath:indexPath];
cell2.label.text = [NSString stringWithFormat:@"%d",indexPath.item];
return cell2;
}
return cell1;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(65, 60);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(10.0f, 10, 10.0f, 10.0f);
}