带有部分的iOS GridView [英] iOS GridView with Sections
问题描述
我试图实现类似于KKGridView的自己的gridview。
我的gridview具有额外的复杂性,因为它需要在每个部分中支持具有不同数量的项目/单元格的多个部分。 (见下)
它还需要能够处理从第1部分到第0部分以及从第0部分到第1部分的一个单元格。
我已经写了UIView来显示网格,但它不支持将单元格从一个部分移动到另一个部分。问题是,任何人都有一个支持此功能的GridView,或者可以告诉我最好的前进方向,因为这个GridView需要在iOS 5& iOS 6
我会尝试使用 UICollectionView
的组合,一个 UICollectionViewFlowLayout
以及 PSTCollectionView
用于iOS5兼容性。
- 看一看
UICollectionViewDelegateFlowLayout
文档,我想你会发现一切您需要根据需要构建每节自定义布局。 -
PSTCollectionView
是一个开放源代码,100%API兼容替代品UICollectionView for iOS4.3 +。此外,您可以在iOS6上使用UICollectionView
,并且只能在iOS5设备上回退到PSTCollectionView
。查看 GitHub项目页面。
我在其中一个正在使用的应用程序中使用这种方法,实施起来非常简单。
编辑:你要做的东西的步骤大致如下:
PSTCollectionView
。为此,请遵循GitHub页面上的步骤。
.h
文件中,导入 PSTCollectionView.h
并添加以下两个协议:< PSUICollectionViewDataSource,PSUICollectionViewDelegate>
。请注意,有了这个,你将在iOS6中使用Apple的 UICollectionView
,在iOS5中使用 PSTCollectionView
。在这里你还应该为你的集合视图添加一个属性: @property(nonatomic,strong)PSUICollectionView * collectionView;
代码:
PSUICollectionViewFlowLayout * flowLayout = [[PSUICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
[flowLayout setItemSize:CGSizeMake(91,119)];
[flowLayout setMinimumLineSpacing:0];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
- 而且您需要按照自己的喜好来实现以下方法:
方法:
numberOfSectionsInCollectionView :
collectionView:numberOfItemsInSection:
collectionView:cellForItemAtIndexPath:
collectionView:layout:insetForSectionAtIndex:
collectionView:layout:minimumLineSpacingForSectionAtIndex:
collectionView:layout:minimumInteritemSpacingForSectionAtIndex:
重要提示:不要忘记在提到 UICollectionView
具有 PS
的类。例如 PSUICollectionView
或 PSUICollectionViewLayout
。编辑2:有关 UICollectionView
的一般理解,请参阅这本优秀的教程。
I am trying to implement my own gridview similar to KKGridView.
My gridview has extra complexity because it needs to support multiple sections with a varying amount of items / cells in each section. (See below)
It also needs to be able to handle moving one cell from section 1 to section 0 and section 0 to section 1.
I have written the UIView to display the grid but it does not support moving cells from one section to another. The question is does anybody have a gridview that supports this functionality or can advise me of the best way forward as this gridview needs to be supported in iOS 5 & iOS 6
I would try using a combination of UICollectionView
, with a UICollectionViewFlowLayout
along with PSTCollectionView
for iOS5 compatibility.
- Take a look at the
UICollectionViewDelegateFlowLayout
docs, I think you'll find everything you need to build a per-section custom layout as you need. PSTCollectionView
is a "open Source, 100% API compatible replacement of UICollectionView for iOS4.3+". Further, you can useUICollectionView
on iOS6 and only fall back toPSTCollectionView
on iOS5 devices. Take a look at the GitHub project page.
I'm using this approach in one of the apps I'm working on, and it is really simple to implement.
EDIT: The steps you'd take to make what you want would be roughly as following:
- You need to download at include the
PSTCollectionView
in your project. For that follow the steps on the GitHub page. - In your
.h
file, importPSTCollectionView.h
and add the following two protocols:<PSUICollectionViewDataSource, PSUICollectionViewDelegate>
. Note that with this you'll use Apple'sUICollectionView
in iOS6 andPSTCollectionView
in iOS5. Here you should also add a property for your collection view:@property (nonatomic, strong) PSUICollectionView *collectionView;
- In the
viewDidLoad:
method, you need to have something like this:
Code:
PSUICollectionViewFlowLayout *flowLayout = [[PSUICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
[flowLayout setItemSize:CGSizeMake(91, 119)];
[flowLayout setMinimumLineSpacing:0];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
- And you'd need to implement the following methods to your liking:
Methods:
numberOfSectionsInCollectionView:
collectionView:numberOfItemsInSection:
collectionView:cellForItemAtIndexPath:
collectionView:layout:insetForSectionAtIndex:
collectionView:layout:minimumLineSpacingForSectionAtIndex:
collectionView:layout:minimumInteritemSpacingForSectionAtIndex:
Important: Don't forget to prefix every mention of UICollectionView
classes with PS
. For example PSUICollectionView
or PSUICollectionViewLayout
.
EDIT 2: For a general understanding of UICollectionView
refer to this excellent tutorial.
这篇关于带有部分的iOS GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!