代码之家  ›  专栏  ›  技术社区  ›  Jack

iPhone SDK中的网格视图[关闭]

  •  19
  • Jack  · 技术社区  · 15 年前

    我想在我的iPhone应用程序中创建一个类似于 iPad photos app . 我知道iPhone3.2sdk是在NDA下的,但是有没有一个库或框架可以添加这种功能(非sdk)。理想情况下,我希望最终开发一个iPad版本的应用程序,其中网格的纵向宽3项,横向宽4项,但目前我希望纵向宽2项,横向宽3项。

    我能想到的唯一方法是通过子类化UITableView,并拥有一个创建2或3项的自定义单元格。不过,这看起来很混乱,我相信还有更好的方法。

    一个典型的项目将有一个图片,标签和按钮-没有什么太复杂。

    谢谢

    6 回复  |  直到 12 年前
        1
  •  9
  •   willcodejavaforfood    15 年前

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

        2
  •  25
  •   catlan    12 年前

    对于iOS 6及以上版本,我建议 UICollectionView PSTCollectionView .

    目标是使用iOS 4/5上的pstcollectionview作为回退,并 切换到iOS6上的uiCollectionView。我们甚至使用某些运行时技巧 在运行时为旧版本的iOS创建uiCollectionView。 理想情况下,只需链接文件,所有内容都可以在较旧版本上工作。 系统。

    2010年我推荐 AQGridView

        3
  •  10
  •   morcutt    13 年前

    我知道这很古老,但我正在寻找答案,并测试了几个解决方案。我发现gmgridview是一个最好的,最少麻烦的解决方案。去看看吧 https://github.com/gmoledina/GMGridView .

        4
  •  2
  •   edzio27    12 年前

    要在表视图中创建简单的网格视图,请创建类“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
    

    当您创建表时,在delegate 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;
    }
    
        5
  •  1
  •   Ahsan    13 年前

    ngridview似乎是最好的解决方法。你可以找到它 here .

    它的方法与UITableView类似。可以根据需要自定义单元格。

        6
  •  -1
  •   AlexR    13 年前

    我建议使用自定义的UITableViewCells。在iOS5中,您不必对它们进行子类化。 只需向项目中添加一个TableView,使用Xcode向TableView添加一个原型单元格(=自定义单元格),给它们一个唯一的标识符,然后在CellForRowatindexPath中使用这个标识符来取消排队并实例化自定义单元格。然后设置单元格并将其返回。