我想在同一页面中构建两个UICollectionView
。两个UICollectionView
将根据需要显示不同的数据。我怎么能这样做?
- (void)viewDidLoad
{
[super viewDidLoad];
self.arr = [[NSMutableArray alloc] init];
for (int i = 0; i < 9; i++) {
[self.arr addObject:[UIImage imageNamed:@"1.png"]];
}
for (int i = 0; i < 9; i++) {
[self.arr2 addObject:[UIImage imageNamed:@"2.png"]];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
self.col = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
[self.col registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"MyCollectionCell"];
[self.col2 registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"CollectionCell"];
UICollectionViewFlowLayout *flowLayout2 = [[UICollectionViewFlowLayout alloc] init];
[flowLayout2 setScrollDirection:UICollectionViewScrollDirectionVertical];
self.col2 = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
[self.col2 registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"MyCollectionCell"];
[self.col setDelegate:self];
[self.scrollView addSubview:self.col];
[self.col2 setDelegate:self];
[self.scrollView addSubview:self.col2];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if ([collectionView isEqual:self.col2])
{
return self.arr2.count;
}
return self.arr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCollectionCell" forIndexPath:indexPath];
if (collectionView == self.col2)
{
cell.imageView.image = [self.arr2 objectAtIndex:indexPath.row];
}
cell.imageView.image = [self.arr objectAtIndex:indexPath.row];
return cell;
}
答案 0 :(得分:2)
您在此处缺少else
声明:
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCollectionCell" forIndexPath:indexPath];
if (collectionView == self.col2) {
cell.imageView.image = [self.arr2 objectAtIndex:indexPath.row];
} else {
cell.imageView.image = [self.arr objectAtIndex:indexPath.row];
}
return cell;
如果这不起作用,您能否更确切地说明您的问题究竟是什么? UICollectionView
之一是否显示或显示错误的数据?
编辑: 好的,我看到你错过了其他两件事:
初始化arr2
:
self.arr2 = [[NSMutableArray alloc] init];
您为UICollectionView
s:
self.col = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
self.col2 = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
您需要定义不同的框架,即:
CGRect frame1 = CGRectMake(0,0,240,320);
CGRect frame2 = CGRectMake(0,320,240,320);
self.col = [[UICollectionView alloc] initWithFrame:frame1 collectionViewLayout:flowLayout];
self.col2 = [[UICollectionView alloc] initWithFrame:frame2 collectionViewLayout:flowLayout];
这只是一个例子,具体取决于您的使用情况。
注意:您应该使用Autolayouts,而不是显式框架。