iOS 中的网格视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2265293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:55:17  来源:igfitidea点击:

Grid view in iOS

iosiphoneuitableviewipadgridview

提问by Hyman

I would like to create a grid view in my iPhone app similar to that shown in the iPad photos app. Is there a library or framework available for adding this kind of functionality (non-SDK)? Ideally I would like to eventually develop an iPad version of the app, where the grid would be 3 items wide in portrait and 4 in landscape, however for the time being I would like 2 items wide in portrait and 3 wide in landscape.

我想在我的 iPhone 应用程序中创建一个类似于iPad 照片应用程序中显示的网格视图。是否有可用于添加此类功能的库或框架(非 SDK)?理想情况下,我希望最终开发该应用程序的 iPad 版本,其中网格的纵向宽度为 3 个项目,横向为 4 个项目,但目前我希望纵向宽度为 2 个项目,横向宽度为 3 个项目。

The only way I can think of doing this is by subclassing UITableView and having a custom cell that creates the 2 or 3 items. This however seems messy and I am sure that there is a better way.

我能想到的唯一方法是继承 UITableView 并拥有一个创建 2 或 3 个项目的自定义单元格。然而,这看起来很混乱,我相信有更好的方法。

A typical item will have a picture, label and button - nothing too complicated.

一个典型的项目会有一个图片、标签和按钮——没有什么太复杂的。

采纳答案by willcodejavaforfood

You can still use the UITableView for this and you would not need to subclass it. Like you said all you have to do is create your own custom cell which it is not complicated. Not messy at all :)

您仍然可以为此使用 UITableView 并且您不需要对其进行子类化。就像你说的,你所要做的就是创建你自己的自定义单元,它并不复杂。一点都不乱:)

回答by catlan

For iOS 6 and above I recommend UICollectionViewand PSTCollectionView.

对于 iOS 6 及更高版本,我推荐UICollectionViewPSTCollectionView

The goal is to use PSTCollectionView on iOS 4/5 as a fallback and switch to UICollectionView on iOS6. We even use certain runtime tricks to create UICollectionView at runtime for older versions of iOS. Ideally, you just link the files and everything works on older systems.

目标是在 iOS 4/5 上使用 PSTCollectionView 作为后备,并在 iOS6 上切换到 UICollectionView。我们甚至使用某些运行时技巧在运行时为旧版本的 iOS 创建 UICollectionView。理想情况下,您只需链接文件,一切都可以在旧系统上运行。

In the year 2010 I recommended AQGridView

2010年我推荐了AQGridView

回答by morcutt

I'm aware this is very old but I was searching for answer to this and tested several solutions. I found GMGridView was one of the best, least buggy solutions. Check it out at https://github.com/gmoledina/GMGridView.

我知道这已经很老了,但我一直在寻找答案并测试了几种解决方案。我发现 GMGridView 是最好的、错误最少的解决方案之一。在https://github.com/gmoledina/GMGridView查看。

回答by edzio27

To make simple grid view in your table view create class "GridViewCell" and in header file add:

要在表视图中创建简单的网格视图,请创建类“GridViewCell”并在头文件中添加:

@interface GridViewCell : UITableViewCell

@property (nonatomic, strong)  UIButton *column1;
@property (nonatomic, strong)  UIButton *column2;
@property (nonatomic, strong)  UIButton *column3;

@end

in .m file add this code:

在 .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

and when you create your table use new class "GridView" in delegate cellForRowAtIndexPath:

当您创建表格时,在委托 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;
}

回答by Ahsan

NRGridView seems to be the best way around. You can find it here.

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

It has the similar methods as UITableView. The cells can be customized as necessary.

它具有与 UITableView 类似的方法。可以根据需要定制单元格。

回答by AlexR

I would recommend using custom UITableViewCells. In iOS 5 you don't have to subclass them. Just add a tableView to your project, add a prototype cells (=custom cell) using Xcode to the tableView, give them a unique identifier and use this identifier in cellForRowAtIndexPath to dequeue and instantiate your custom cell. Then set the cell and return it.

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