iOS 中的网格视图[关闭]
-
20-09-2019 - |
题
我想在我的 iPhone 应用程序中创建一个网格视图,类似于 iPad 照片应用程序. 。是否有可用于添加此类功能的库或框架(非 SDK)?理想情况下,我希望最终开发出该应用程序的 iPad 版本,其中网格在纵向上为 3 个项目宽,在横向上为 4 个项目宽,但目前我希望纵向上有 2 个项目宽,横向上有 3 个项目宽。
我能想到的唯一方法是对 UITableView 进行子类化并使用一个自定义单元来创建 2 或 3 个项目。然而,这看起来很混乱,我确信有更好的方法。
典型的项目将有图片、标签和按钮 - 没什么太复杂的。
解决方案
您仍可以使用此UITableView的,你不会需要它的子类。就像你说你要做的就是创造它并不复杂自己的自定义单元格。不杂乱在所有:)
其他提示
我知道这是很老,但我正在寻找答案,这和测试了多种解决方案。我发现GMGridView是最好的,至少马车的解决方案之一。检查它在 https://github.com/gmoledina/GMGridView 。
要进行简单的网格视图在表视图中创建类“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
当你创建你的表使用委托的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似乎周围的最好方式。你可以找到它这里。
它具有类似的方法作为UITableView的。该细胞可被定制为必要的。
我建议使用自定义UITableViewCells。在iOS 5中,你不必继承他们。 只需添加的tableView到项目中,添加一个原型细胞(=自定义单元格)使用的Xcode到的tableView,给他们一个唯一的标识符,使用该标识符的cellForRowAtIndexPath出列和实例化您的自定义单元格。然后设置细胞并将其返回。