【问题标题】:how i can create collection view like in this image我怎样才能像这张图片一样创建收藏视图
【发布时间】:2015-12-12 15:06:15
【问题描述】:

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

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

【问题讨论】:

  • 您的开始是错误的 - 您不应该使用固定尺寸,而是根据要显示的图像尺寸使用动态尺寸。还有更好的变量命名,small、k和j是干什么的?
  • 我不需要图像大小的单元格,我需要固定前两个小,而不是两个大
  • 我对此表示怀疑。您实际上只需要阅读有关如何使用集合视图委托方法的内容。其余的都很简单直接。
  • 你能帮我添加一些代码吗?

标签: ios objective-c uicollectionview


【解决方案1】:

支持@beyowulf的回答我为这个问题写了一些代码,你可以find that here,查看CollectionViewTest部分:

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

看起来像这样:

【讨论】:

    【解决方案2】:

    我不确定你的其余代码在做什么,但如果你想要的只是两个小单元格然后是两个大单元格,你可以这样做:

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

    虽然我会建议返回

    【讨论】:

      猜你喜欢
      • 2017-07-06
      • 1970-01-01
      • 1970-01-01
      • 2020-06-27
      • 2021-06-21
      • 2016-06-04
      • 1970-01-01
      • 1970-01-01
      • 2022-11-04
      相关资源
      最近更新 更多