我如何创建此图像中的集合视图 [英] how i can create collection view like in this image

查看:20
本文介绍了我如何创建此图像中的集合视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要第一个两个单元格小第二个两个大又两个小两个大.我曾尝试使用下面的代码.但它没有给我正确的输出.其他任何易于实现的第三方库.急切地等待这个问题的答案.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{如果(collectionView == _CollectionSale){如果(小&& j != 2){j++;如果(j==2){小 = 假;k = 0;}HomeSaleCC *objHomeSaleCC=[collectionView dequeueReusableCellWithReuseIdentifier:@"HomeSaleCC" forIndexPath:indexPath];返回 objHomeSaleCC;}else if(!small && k !=2){k++;如果(k==2){小 = 真;j = 0;}HomeSaleCC2 *objHomeSaleCC2=[collectionView dequeueReusableCellWithReuseIdentifier:@"HomeSaleCC2" forIndexPath:indexPath];返回 objHomeSaleCC2;}HomeSaleCC *objHomeSaleCC=[collectionView dequeueReusableCellWithReuseIdentifier:@"HomeSaleCC" forIndexPath:indexPath];返回 objHomeSaleCC;}别的{HomeTabCC *objHomeTabCC=[collectionView dequeueReusableCellWithReuseIdentifier:@"HomeTabCC" forIndexPath:indexPath];[objHomeTabCC.btnSale setTitle:[arrTitle objectAtIndex:indexPath.row] forState:UIControlStateNormal];[objHomeTabCC.btnSale setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];如果(indexPath.row == i){objHomeTabCC.viewLine.hidden =NO;objHomeTabCC.btnSale.selected =YES;}别的{objHomeTabCC.viewLine.hidden = 是;objHomeTabCC.btnSale.selected =NO;}返回 objHomeTabCC;}}- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{如果(collectionView == _CollectionSale){如果(小&& j != 2){j++;如果(j==2){小 = 假;k = 0;}返回 CGSizeMake(153, 186);}否则 if(!small && k !=2){k++;如果(k==2){小 = 真;j = 0;}返回 CGSizeMake(260, 186);}别的{返回 CGSizeMake(260, 186);}}别的{返回 CGSizeMake(260, 186);}}

解决方案

Supporting @beyowulf's answer 我为这个问题写了一些代码,你可以

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);
    }
}

解决方案

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:

这篇关于我如何创建此图像中的集合视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆