在iPhone SDK的网格视图

我想在我的iPhone应用程序中创build类似于iPad照片应用程序中显示的网格视图。 我知道iPhone 3.2 SDK是在NDA下,但有没有一个库或框架可用于添加这种function(非SDK)。 理想情况下,我想最终开发一个应用程序的iPad版本,其中的网格将是3个项目宽的肖像和4个景观,但是暂时我想要2个项目宽的肖像和3个宽的风景。

我能想到的唯一方法是通过inheritanceUITableView,并创build一个自定义单元格来创build2或3个项目。 然而,这似乎很混乱,我相信有一个更好的办法。

一个典型的项目将有一个图片,标签和button – 没有太复杂。

谢谢

你仍然可以使用这个UITableView,你不需要inheritance它。 就像你所说的,你所要做的就是创build你自己的自定义单元格,它并不复杂。 没有杂乱的一切:)

对于iOS 6及以上版本,我推荐使用UICollectionView和PSTCollectionView 。

目标是在iOS 4/5上使用PSTCollectionView作为回退,并切换到iOS6上的UICollectionView。 我们甚至使用某些运行时间技巧在运行时为较旧版本的iOS创buildUICollectionView。 理想情况下,你只需链接文件和一切在旧系统上的作品。

在2010年,我推荐了AQGridView

我知道这是非常古老的,但我正在寻找答案,并testing了几种解决scheme。 我发现GMGridView是最好的,最less量的解决scheme之一。 在https://github.com/gmoledina/GMGridView中查看。;

为了在你的表格视图中创build简单的网格视图创build类“GridViewCell”,并在头文件中添加:

@interface GridViewCell : UITableViewCell @property (nonatomic, strong) UIButton *column1; @property (nonatomic, strong) UIButton *column2; @property (nonatomic, strong) UIButton *column3; @end 

在.m文件中添加以下代码:

 #define CELL_WIDTH 100 #define CELL_HEIGHT 80 #import "GridViewCell.h" @implementation GridViewCell @synthesize column1, column2, column3; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { column1 = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, CELL_WIDTH, CELL_HEIGHT)]; [self addSubview:column1]; column2 = [[UIButton alloc] initWithFrame:CGRectMake(CELL_WIDTH+ 10, 5, CELL_WIDTH, CELL_HEIGHT)]; [self addSubview:column2]; column3 = [[UIButton alloc] initWithFrame:CGRectMake(CELL_WIDTH + CELL_WIDTH + 15, 5, CELL_WIDTH, CELL_HEIGHT)]; [self addSubview:column3]; } return self; } @end 

当你创build你的表时,在委托cellForRowAtIndexPath中使用新的类“GridView”:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; GridViewCell *cell = (GridViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[GridViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } [cell.column1 setBackgroundColor:[UIColor blackColor]]; [cell.column2 setBackgroundColor:[UIColor blackColor]]; [cell.column3 setBackgroundColor:[UIColor blackColor]]; return cell; } 

NRGridView似乎是最好的方法。 你可以在这里find它。

它有和UITableView类似的方法。 细胞可以根据需要进行定制。

我会build议使用自定义的UITableViewCells。 在iOS 5中,您不必将其子类化。 只需要添加一个tableView到你的项目中,使用Xcode添加一个原型单元格(=自定义单元格)到tableView,给它们一个唯一的标识符,并在cellForRowAtIndexPath中使用这个标识符来出队和实例化你的自定义单元格。 然后设置单元格并将其返回。