2014-12-23 97 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; 

填寫的要求,並將其添加爲單元格的內容視圖的子視圖:然後,創建一個標籤放在細胞。

我有很多的調整工作要做,但是在幾個小時的環視後,它會相當接近。

相關問題