2014-12-23 98 views
0

我是ios开发的新手。我搜索了许多使用许可证密钥的数据网格框架的教程。我想在给定的link处显示类似此表的数据。我如何使用uicollection视图来显示链接中的表格数据? 在gridview.h文件的代码是:如何使用uicollectionview在表格中显示数据

进口
@interface GridViewCell : UITableViewCell 

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

@end 

在gridview.m文件中的代码是:

// 
// GridViewCell.m 
// SQLite3DBSample 
// 
// Created by Dezine House on 22/12/2014. 
// Copyright (c) 2014 Bilal ARSLAN. All rights reserved. 
// 
#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) { 
     // Initialization code 

      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; 
} 

- (void)awakeFromNib 
{ 
    // Initialization code 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 




- (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; 
} 

@end 
+2

欢迎来到Stack Overflow!我想你已经尝试了一些东西。如果是这样,那么请[编辑](http://stackoverflow.com/posts/27615866/edit)您的文章并添加您的代码,以便我们可以使用一些东西。请参阅[tour](http://stackoverflow.com/tour)并阅读[如何提问](http://stackoverflow.com/questions/how-to-ask)以了解我们对问题的期望。 – honk

回答

0

有关创建的n×m个栅格使用的CollectionView代表

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 
    return n; 
} 

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ 
    return m; 
} 

然后,为了创建它们的特定大小,使用代理方法Coll ectionView返回布局的每个细胞中的CollectionView

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    //for bigger cell 
    if (indexPath.row == 0) { 
     return CGSizeMake(120, 44); 
    } 
    //for smaller ones 
    else{ 
     return CGSizeMake(44, 44); 
    } 
} 

希望你知道如何在IOS

0

我一直在努力做这样的事情使用collectionView,虽然我还没说完用它修补,这并不难。子类化UICollectionViewCell和UICollectionViewFlowLayout可能是最干净的,但我还没有发现它是必要的。我到目前为止最大的问题是让单元格边框看起来像我想要的。我设想可能有必要对这两者中的一个或两者进行子类化,但到目前为止,它并不需要我的VC中有太多的代码,所以我还没有完成它。

除了通常的UICollectionView数据源/委托,您还想添加UICollectionViewDelegateFlowLayout。在我的情况下,我使我的数据中的每个部分与表中的一行对应,并且indexPath中的每个“行”与表中的列一致。它似乎工作。然后,执行下面的方法,让您在部分小区的每一列的大小(看indexPath.row值来确定你正在使用哪一列):

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 

在你cellForItemAtIndexPath为我的应用是足够简单以计算列号如下(我的表中有六列):

NSInteger col = indexPath.row % 6; 

所以,山口== 0是第一列,列== 5是第六列。在标签

UILabel *label = [[UILabel alloc] initWithFrame:cell.bounds; 

填写的要求,并将其添加为单元格的内容视图的子视图:然后,创建一个标签放在细胞。

我有很多的调整工作要做,但是在几个小时的环视后,它会相当接近。

相关问题