how i can create collection view like in this imag

2019-08-17 07:06发布

问题:

i need first two cell small second two large again two small two large. i had tried with below code.but its not giving me proper output.else any third party library that is easy to implement for this.eagerly waiting for answer of this question.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == _CollectionSale) {
        if (small && j != 2){
            j++;
            if(j==2){
                small = false;
                k = 0;

            }
            HomeSaleCC *objHomeSaleCC=[collectionView dequeueReusableCellWithReuseIdentifier:@"HomeSaleCC" forIndexPath:indexPath];
            return objHomeSaleCC;
        }else if(!small && k !=2){
             k++;
            if(k==2){
                small = true;
                j = 0;
            }
            HomeSaleCC2 *objHomeSaleCC2=[collectionView dequeueReusableCellWithReuseIdentifier:@"HomeSaleCC2" forIndexPath:indexPath];
            return objHomeSaleCC2;
        }

        HomeSaleCC *objHomeSaleCC=[collectionView dequeueReusableCellWithReuseIdentifier:@"HomeSaleCC" forIndexPath:indexPath];
        return objHomeSaleCC;


    }
    else{

    HomeTabCC *objHomeTabCC=[collectionView dequeueReusableCellWithReuseIdentifier:@"HomeTabCC" forIndexPath:indexPath];
    [objHomeTabCC.btnSale setTitle:[arrTitle objectAtIndex:indexPath.row] forState:UIControlStateNormal];
    [objHomeTabCC.btnSale setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    if (indexPath.row == i) {
        objHomeTabCC.viewLine.hidden =NO;
        objHomeTabCC.btnSale.selected =YES;
    }
    else{
        objHomeTabCC.viewLine.hidden =YES;
        objHomeTabCC.btnSale.selected =NO;
    }
    return objHomeTabCC;
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == _CollectionSale) {
        if (small && j != 2){
            j++;
            if(j==2){
                small = false;
                k = 0;
            }
            return CGSizeMake(153, 186);
        }

    else if(!small && k !=2){
        k++;
        if(k==2){
            small = true;
            j = 0;
        }
        return CGSizeMake(260, 186);
    }else{
        return CGSizeMake(260, 186);
    }

    }
    else{
        return CGSizeMake(260, 186);
    }
}

回答1:

Supporting @beyowulf's answer I wrote some code for this problem, you can find that here, check into CollectionViewTest section:

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return 7;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"smallCell" forIndexPath:indexPath];
    [cell.contentView setBackgroundColor:[UIColor redColor]];

    if (indexPath.row%4 < 2) {
        [cell.contentView setBackgroundColor:[UIColor greenColor]];
    }
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row % 4 < 2) {
        return CGSizeMake([UIScreen mainScreen].bounds.size.width / 2 - 15.0f, 80.0f);
    }
    return CGSizeMake([UIScreen mainScreen].bounds.size.width / 2 - 15.0f, 200.0f);
}

It look something like this:



回答2:

I'm not sure what the rest of your code is doing, but if all you want is two small cell then two large cells, you can do it thusly:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.item % 4 < 2) {

            return CGSizeMake(collectionView.bounds.size.width*0.5-someSpacing, 186);
        }
        return CGSizeMake(collectionView.bounds.size.width*0.5-someSpacing, 186*2);
}

Though I would recommend returning