Ios UICollectionView带有两个单元格的自定义布局

Ios UICollectionView带有两个单元格的自定义布局,ios,objective-c,uitableview,uicollectionviewcell,uicollectionviewlayout,Ios,Objective C,Uitableview,Uicollectionviewcell,Uicollectionviewlayout,我一直在阅读关于UICollectionView的不同布局的在线教程。我也看了很多关于这个主题的问题。但看起来我所期待的可能是更简单的事情,但我被困在如何前进 self.minimumInteritemSpacing = 0.1f' 目标 self.minimumInteritemSpacing = 0.1f' 我有一个UIViewController,它嵌入在UINavigation控制器中。我在UITableView中显示数据,其中包括:1个UIImageView和每个单元格中的3个UI

我一直在阅读关于
UICollectionView
的不同布局的在线教程。我也看了很多关于这个主题的问题。但看起来我所期待的可能是更简单的事情,但我被困在如何前进

self.minimumInteritemSpacing = 0.1f'
目标

self.minimumInteritemSpacing = 0.1f'
我有一个
UIViewController
,它嵌入在
UINavigation
控制器中。我在UITableView中显示数据,其中包括:1个UIImageView和每个单元格中的3个UILabel。数据是从服务器获取的,所有的工作都很好

self.minimumInteritemSpacing = 0.1f'
然后我想要一个UIButton,当点击它时,它会启动一个很酷的动画,显示细胞过渡到一个漂亮的网格视图

self.minimumInteritemSpacing = 0.1f'
我突然意识到,我需要使用
UICollectionView
在这两个单元格之间切换,并完全抛弃UITableView。再次点击按钮,将切换回上一个状态(网格或
UITableView
style)

self.minimumInteritemSpacing = 0.1f'
网格单元需要松开一个标签,但保留图像

self.minimumInteritemSpacing = 0.1f'
问题

self.minimumInteritemSpacing = 0.1f'
过去两天我一直在阅读
UICollectionView
UICollectionViewFlowLayout
。我想我可以使用苹果公司预先制作的
UICollectionViewFlowLayout
,然后稍微调整一下

self.minimumInteritemSpacing = 0.1f'
我不知道我是否需要两个自定义单元格或一个在两个视图之间改变形状的单元格,以及动画必须如何工作

self.minimumInteritemSpacing = 0.1f'
我不是在寻找实现这一点的确切代码——我只是需要知道我需要进入哪个方向,以及是否需要使用两个自定义单元格——以及如何通过动画在这两个单元格之间进行更改,而不必再次重新加载所有数据

self.minimumInteritemSpacing = 0.1f'
感谢您的任何意见

self.minimumInteritemSpacing = 0.1f'

谢谢大家

我终于找到了一个满足我需要的解决方案。如果任何人有类似的需求-这就是如何使用两个不同的自定义
UICollectionViewCell
,以及如何在两个不同的单元格/布局之间进行更改

self.minimumInteritemSpacing = 0.1f'
  • 第一件事是在IB中创建CustomCell-创建
    xib
    档案
  • 然后根据需要设置
因为我的需求需要类
UICollectionViewFlowLayout
提供的标准流布局,所以我只需要创建两个自定义布局,并根据需要调整它们

self.minimumInteritemSpacing = 0.1f'
  • 创建两个(或更多,如果需要)子类
    UICollectionViewFlowLayout的类
    
在实现中-根据需要设置布局。因为我正在对预先制作的UICollectionViewFlowLayOut进行子类化,所以我所需要做的就是调整它——实现非常简单

self.minimumInteritemSpacing = 0.1f'
所以-对于表视图布局,我做了以下操作:

tableViewFlowLayOut.m

-(id)init
{
    self = [super init];

    if (self){

        self.itemSize = CGSizeMake(320, 80);
        self.minimumLineSpacing = 0.1f;
    }

    return self;
}
self.minimumInteritemSpacing = 0.1f'
这将每个单元格的宽度和高度设置为所需的值<代码>自身。最小行间距设置单元格之间的间距。(上方/下方单元格之间的间距)

self.minimumInteritemSpacing = 0.1f'
然后,对于网格布局:

gridFlowLayOut.m

-(id)init
{
    self = [super init];

    if (self){

        self.itemSize = CGSizeMake(159, 200);
        self.minimumInteritemSpacing = 0.1f;
        self.minimumLineSpacing = 0.1f;
    }
    return self; 
}
self.minimumInteritemSpacing = 0.1f'
和以前一样-但是,这次我需要我的细胞之间的间距-右边缘-

self.minimumInteritemSpacing = 0.1f'
处理好了

self.minimumInteritemSpacing = 0.1f'
现在-将其放在一起-在具有
UICollectionView

self.minimumInteritemSpacing = 0.1f'
viewController.m

// Import the new layouts needed. 

#import "GridFlowLayOut.h"
#import "TableViewFlowLayOut.m"

//Create the properties 

@property (strong, nonatomic) TableViewFlowLayOut *tableViewLayout;
@property (strong, nonatomic) GridFlowLayOut *grideLayout;

-(void)viewDidLow
{
//Register the two custom collection view cells you created earlier. Make sure you set the correct reuse identifier here. 

[self.tradeFeedCollectionView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil] forCellWithReuseIdentifier:@"TableItemCell"];
    [self.tradeFeedCollectionView registerNib:[UINib nibWithNibName:@"GridViewCell" bundle:nil] forCellWithReuseIdentifier:@"GridItemCell"];

}

-(void)viewWillAppear
{
//Create the layout objects

         self.grideLayout = [[GridFlowLayOut alloc]init];
    self.tableViewLayout = [[TableViewFlowLayOut alloc]init];

//Set the first layout to what it should be 

    [self.tradeFeedCollectionView setCollectionViewLayout:self.tableViewLayout];

}
立即使用动画在布局之间进行更改。这实际上很容易做到,只需要几行代码-

self.minimumInteritemSpacing = 0.1f'
我在
viewController.m

self.minimumInteritemSpacing = 0.1f'
-(void)changeViewLayoutButtonPressed
{
//BOOl value to switch between layouts 

    self.changeLayout = !self.changeLayout;

    if (self.changeLayout){
    [self.tradeFeedCollectionView setCollectionViewLayout:self.grideLayout animated:YES];

    }

    else {

          [self.tradeFeedCollectionView setCollectionViewLayout:self.tableViewLayout animated:YES];
    }
}
最后在
cellForItemAtIndexPath

self.minimumInteritemSpacing = 0.1f'
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{   static NSString *tableCellIdentifier = @"TableItemCell";
    static NSString *gridCellIdentifier = @"GridItemCell";

//BOOL used to detect which layout is active
    if (self.gridLayoutActive == NO){

        CustomCollectionCellClass *tableItemCell = [collectionView dequeueReusableCellWithReuseIdentifier:tableCellIdentifier forIndexPath:indexPath];

    //Setup the cell

    }
        return tableItemCell;
}else
    {

        CustomCollectionCellClass *gridItemCell= [collectionView dequeueReusableCellWithReuseIdentifier:gridCellIdentifier forIndexPath:indexPath];

    //Setup the cell

    }

    return gridItemCell;
    }
        return nil;

}
当然,您需要遵守其他UICollectionView委托并设置其余内容

self.minimumInteritemSpacing = 0.1f'
这实际上花了我一段时间才弄明白。我真的希望它能帮助其他人。 如果有人想要一个演示项目,我很乐意创建一个并上传到GitHub

self.minimumInteritemSpacing = 0.1f'
对于任何刚接触UICollectionViews的人,我强烈建议阅读苹果公司关于这个主题的编程指南——正是这份文档引导我找到了这个解决方案

self.minimumInteritemSpacing = 0.1f'
参考:

我终于找到了一个可以满足我需求的解决方案。如果任何人有类似的需求-这就是如何使用两个不同的自定义
UICollectionViewCell
,以及如何在两个不同的单元格/布局之间进行更改

self.minimumInteritemSpacing = 0.1f'
  • 第一件事是在IB中创建CustomCell-创建
    xib
    档案
  • 然后根据需要设置
因为我的需求需要类
UICollectionViewFlowLayout
提供的标准流布局,所以我只需要创建两个自定义布局,并根据需要调整它们

self.minimumInteritemSpacing = 0.1f'
  • 创建两个(或更多,如果需要)子类
    UICollectionViewFlowLayout的类
    
在实现中-根据需要设置布局。因为我正在对预先制作的UICollectionViewFlowLayOut进行子类化,所以我所需要做的就是调整它——实现非常简单

self.minimumInteritemSpacing = 0.1f'
所以-对于表视图布局,我做了以下操作:

tableViewFlowLayOut.m

-(id)init
{
    self = [super init];

    if (self){

        self.itemSize = CGSizeMake(320, 80);
        self.minimumLineSpacing = 0.1f;
    }

    return self;
}
self.minimumInteritemSpacing = 0.1f'
这将每个单元格的宽度和高度设置为所需的值<代码>自身。最小行间距设置单元格之间的间距。(上方/下方单元格之间的间距)

self.minimumInteritemSpacing = 0.1f'
然后,对于网格布局:

gridFlowLayOut.m

-(id)init
{
    self = [super init];

    if (self){

        self.itemSize = CGSizeMake(159, 200);
        self.minimumInteritemSpacing = 0.1f;
        self.minimumLineSpacing = 0.1f;
    }
    return self; 
}
self.minimumInteritemSpacing = 0.1f'
和以前一样-但是,这次我需要我的细胞之间的间距-右边缘-

self.minimumInteritemSpacing = 0.1f'
处理好了

self.minimumInteritemSpacing = 0.1f'
现在-将其放在一起-在具有
UICollectionView

self.minimumInteritemSpacing = 0.1f'
viewController.m

// Import the new layouts needed. 

#import "GridFlowLayOut.h"
#import "TableViewFlowLayOut.m"

//Create the properties 

@property (strong, nonatomic) TableViewFlowLayOut *tableViewLayout;
@property (strong, nonatomic) GridFlowLayOut *grideLayout;

-(void)viewDidLow
{
//Register the two custom collection view cells you created earlier. Make sure you set the correct reuse identifier here. 

[self.tradeFeedCollectionView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil] forCellWithReuseIdentifier:@"TableItemCell"];
    [self.tradeFeedCollectionView registerNib:[UINib nibWithNibName:@"GridViewCell" bundle:nil] forCellWithReuseIdentifier:@"GridItemCell"];

}

-(void)viewWillAppear
{
//Create the layout objects

         self.grideLayout = [[GridFlowLayOut alloc]init];
    self.tableViewLayout = [[TableViewFlowLayOut alloc]init];

//Set the first layout to what it should be 

    [self.tradeFeedCollectionView setCollectionViewLayout:self.tableViewLayout];

}
立即使用动画在布局之间进行更改。这实际上很容易做到,只需要几行代码-

self.minimumInteritemSpacing = 0.1f'
我在
viewController.m

self.minimumInteritemSpacing = 0.1f'
-(void)changeViewLayoutButtonPressed
{
//BOOl value to switch between layouts 

    self.changeLayout = !self.changeLayout;

    if (self.changeLayout){
    [self.tradeFeedCollectionView setCollectionViewLayout:self.grideLayout animated:YES];

    }

    else {

          [self.tradeFeedCollectionView setCollectionViewLayout:self.tableViewLayout animated:YES];
    }
}
最后在
cellForItemAtIndexPath

self.minimumInteritemSpacing = 0.1f'
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{   static NSString *tableCellIdentifier = @"TableItemCell";
    static NSString *gridCellIdentifier = @"GridItemCell";

//BOOL used to detect which layout is active
    if (self.gridLayoutActive == NO){

        CustomCollectionCellClass *tableItemCell = [collectionView dequeueReusableCellWithReuseIdentifier:tableCellIdentifier forIndexPath:indexPath];

    //Setup the cell

    }
        return tableItemCell;
}else
    {

        CustomCollectionCellClass *gridItemCell= [collectionView dequeueReusableCellWithReuseIdentifier:gridCellIdentifier forIndexPath:indexPath];

    //Setup the cell

    }

    return gridItemCell;
    }
        return nil;

}
当然,您需要遵守其他UICollectionView委托并设置其余内容

self.minimumInteritemSpacing = 0.1f'
这实际上花了我一段时间才弄明白。我真的希望它能帮助其他人。 如果有人想要一个演示项目,我很乐意创建一个并上传到GitHub

self.minimumInteritemSpacing = 0.1f'
对于任何刚接触UICollectionViews的人,我强烈建议他们阅读苹果关于这个主题的编程指南——正是这份文档