Search code examples
objective-cios7uicollectionviewuicollectionviewcell

How to create UICollectionView with alternating cell amount layout?


I am developing an iPad app that uses UICollectionView to display a list of products from a company. The cells layout that I want to use is alternating between rows with 1 image and 2 images, like first row 1 image, second row 2 images, third row 1 image... and so on, but I just can't figure out how to do this in code.

So can anyone help me with this one?


Solution

  • Hey guys I manage to get it working with this code:

    - (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
    
    return 2;
    }
    
    - (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
    return 30;
    }
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    Cells *cell;
    
    if (indexPath.row %2) {
        NSLog(@"%li", indexPath.row);
        cell = [cv dequeueReusableCellWithReuseIdentifier:@"one" forIndexPath:indexPath];
        cell.label.text = @"One";
        cell.backgroundColor = [UIColor blueColor];
        return cell;
    } else {
        NSLog(@"%li", indexPath.row);
        cell = [cv dequeueReusableCellWithReuseIdentifier:@"two" forIndexPath:indexPath];
        cell.labelA.text = @"Two";
        cell.labelB.text = @"Two";
        cell.backgroundColor = [UIColor redColor];
        return cell;
    }
    
    }