首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在集合视图中渲染3个自定义视图

在集合视图中渲染3个自定义视图
EN

Stack Overflow用户
提问于 2016-03-18 20:03:07
回答 1查看 75关注 0票数 0

我使用CollectionViewController来渲染3个自定义视图,第一个是uiView,第二个是uiView,第三个是grid.But,现在的问题是我不能在another.Its重叠后渲染所有三个视图,在每个other.Please帮助中我做错了。我必须为我的应用程序创建相同的设计。

http://a2.mzstatic.com/us/r30/Purple69/v4/59/10/fd/5910fd12-0721-9473-8001-8d3d16b22bdb/screen322x572.jpeg

同时检查我的代码。

代码语言:javascript
复制
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *identifier = @"Cell";

    UICollectionViewCell *cell;
    if (indexPath.section==0)

    {

        HeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HeaderCollectionViewCell" forIndexPath:indexPath];
        cell.frame=CGRectMake(0, 0, self.view.frame.size.width,200);
        cell.backgroundColor=[UIColor redColor] ;

        return cell;
    }

    else if(indexPath.section==1)

    {

        HeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HeaderCollectionViewCell" forIndexPath:indexPath];
        cell.frame=CGRectMake(0, 0, self.view.frame.size.width,200);
        cell.backgroundColor=[UIColor orangeColor];

        return cell;

    }

    else if(indexPath.section==2)

    {

        cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    }



    //   UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    // recipeImageView.image = [UIImage imageNamed:[recipeImages objectAtIndex:indexPath.row]];
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]];

    // Configure the cell
    return cell;

}
EN

回答 1

Stack Overflow用户

发布于 2016-03-18 22:17:32

如果你想要这样的设计,你可能需要修改一些东西。

我会从忘记UICollectionViewController业务开始。只需设置一个常规UIViewController并在其中插入一个UICollectionView即可。代码如下:

代码语言:javascript
复制
    UICollectionViewFlowLayout *flow = [UICollectionViewFlowLayout new];
    flow.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flow.minimumLineSpacing = 0.0f;
    flow.minimumInteritemSpacing = 0.0f;
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;

    cv = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40, w, h-40) collectionViewLayout:flow];
    cv.delegate = self
    cv.dataSource = self;
    cv.scrollEnabled = true;
    cv.bounces = true;
    cv.showsVerticalScrollIndicator = false;
    cv.backgroundColor = [UIColor clearColor];
    [cv registerClass:[customCell class] forCellWithReuseIdentifier:@"customCell"];
    [self.view addSubview:cv];

W和h只是屏幕宽度和高度的变量。您还需要将以下内容添加到您的头文件:

代码语言:javascript
复制
<UICollectionViewDataSource, UICollectionViewDelegate>

现在CV想要调用下面的方法,如果它没有得到它们,它会报错:

代码语言:javascript
复制
- (CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

  if(indexPath.section == 0){ 

    //then return a large cell size e.g.
    return CGSizeMake(320, 200);

  } else if (indexPath.section == 1) { 

    //then return a square grid size, e.g.
    return CGSizeMake(160, 160);

  }

 return CGSizeZero;

}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0.0;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0,0,0,0);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return sectionZeroArray.count;
     } else ...     
}
 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}

包括cellForItem方法,它更多地用于显示单元格中的内容,通常连接到数据对象数组。你不能在里面设定大小,就像你现在拥有的那样。

但是考虑到你想要做的事情,也许更好的做法是利用uicollectionview的页眉和页脚,而不是按节分割。

所以你只有一个部分,在其中你将拥有所有网格大小的正方形单元格,然后在最上面的部分你将有一个集合视图标题,在这个标题中,粘贴一个启用分页的UIScrollView,连接到UIPageControl。

我不知道你想做什么,在底部的第三部分,但你可以遵循相同的程序与头部视图,但当然如你所愿。

如果你已经添加了所有这些方法,但你仍然在苦苦挣扎,那就发布你的代码吧。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36083867

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档