如何在同一页面上拥有两个集合视图

时间:2016-01-05 02:46:43

标签: objective-c uicollectionview

我想在同一页面中构建两个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;
}

1 个答案:

答案 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之一是否显示或显示错误的数据?

编辑: 好的,我看到你错过了其他两件事:

  1. 初始化arr2

    self.arr2 = [[NSMutableArray alloc] init];
    
  2. 您为UICollectionView s:

    定义了相同的框架
    self.col  = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
    self.col2 = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
    
  3. 您需要定义不同的框架,即:

    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,而不是显式框架。