我们知道在ios6以后,系统多了个UICollectionView可以显示多列数据,就像android的gridview,那么在ios6以前,我们如何实现呢?这里,我们通过一个tableview来实现。
它的原理是这样的,总共有多少条数据,除以要显示的列数,用来定义cell的行数,然后在一行中初始化这行中有多少列数据。。。。。。。
写个简单点的DEMO。。。结构如下:
这里,我们先定义一个头文件,InfoConfig.h,在这个头文件里我们定义相关的宏。。。
- #ifndef TestTableTwoRow_InfoConfig_h
- #define TestTableTwoRow_InfoConfig_h
- #define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
- #define rowcellCount 2
- #define RMarginX 0
- #define RMarginY 0
- #endif
然后我们定义一个数据模型SubCollectionsInfo类,这里只是封装了一些属性,提供set/get方法。。。。
- @interface SubCollectionsInfo : NSObject
- {
- NSString *_iconString;
- NSString *_niandaiString;
- NSString *_titleString;
- NSString *_contentString;
- NSString *_xmlFile;
- NSString *_idString;
- NSString *_nbidString;
- NSString *_timenmString;
- NSString *_stimeString;
- NSString *_etimeString;
- NSString *_zipFile;
- NSString *_md5String;
- }
- @property(nonatomic,copy) NSString *iconString;
- @property(nonatomic,copy) NSString *niandaiString;
- @property(nonatomic,copy) NSString *titleString;
- @property(nonatomic,copy) NSString *xmlFile;
- @property(nonatomic,copy) NSString *idString;
- @property(nonatomic,copy) NSString *contentString;
- @property(nonatomic,copy) NSString *nbidString;
- @property(nonatomic,copy) NSString *timenmString;
- @property(nonatomic,copy) NSString *stimeString;
- @property(nonatomic,copy) NSString *etimeString;
- @property(nonatomic,copy) NSString *zipFile;
- @property(nonatomic,copy) NSString *md5String;
- @property(nonatomic,assign) BOOL isselected;
- @property(nonatomic,assign) BOOL isdownload;
- @end
然后我们自定义一个tablecell继承于UITableViewCell,,,封装一个view,用来展示我们的界面
。。。。。具体可以看代码
最后,我们初始化一个tableview,加载相应的数据上去。。。
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- if (iPhone5) {
- self.view.frame=CGRectMake(0,0, 320, 568);
- }else
- {
- self.view.frame=CGRectMake(0,0, 320, 480);
- }
- [self.view setBackgroundColor:[UIColor whiteColor]];
- self.navigationItem.title=@"大帝";
- [self initData];
- [self initUI];
- }
- -(void) initData
- {
- dataArray=[[NSMutableArray alloc] init];
- for (int i=0; i<10; i++)
- {
- SubCollectionsInfo *info=[[SubCollectionsInfo alloc] init];
- info.niandaiString=@"王子大人";
- info.titleString=@"大帝";
- info.contentString=@"好人";
- info.iconString=@"celltouxiang.jpg";
- [dataArray addObject:info];
- [info release];
- }
- }
- -(void) initUI
- {
- testTableView=[[UITableView alloc] init];
- [testTableView setFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
- testTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
- [testTableView setBackgroundColor:[UIColor clearColor]];
- [self.view addSubview:testTableView];
- testTableView.dataSource=self;
- testTableView.delegate=self;
- }
- -(void)cellviewTaped:(UITapGestureRecognizer *)recognizer
- {
- int tag=[recognizer view].tag-8000;
- NSLog(@"%d",tag);
- }
- #pragma tableview
- -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return (dataArray.count-1)/rowcellCount+1;
- }
- -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier1 = @"Cell1";
- SubCollectionsCell2 *cell =(SubCollectionsCell2 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
- if (cell == nil)
- {
- cell = [[[SubCollectionsCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
- cell.backgroundColor=[UIColor clearColor];
- }
- NSUInteger row=[indexPath row];
- SubCollectionsInfo *subInfo=nil;
- for (NSInteger i = 0; i < rowcellCount; i++)
- {
- //奇数
- if (row*rowcellCount+i>dataArray.count-1)
- {
- break;
- }
- subInfo=[dataArray objectAtIndex:row*rowcellCount + i];
- if (i==0)
- {
- [cell.cellView1.iconImageView setImage:[UIImage imageNamed:subInfo.iconString]];
- [cell.cellView1.niandaiLabel setText:subInfo.niandaiString];
- [cell.cellView1.titleLabel setText:subInfo.titleString];
- [cell.cellView1.contentLabel setText:subInfo.contentString];
- cell.cellView1.tag=8000+row*rowcellCount + i;
- UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellviewTaped:)];
- [ cell.cellView1 addGestureRecognizer:tapRecognizer];
- [tapRecognizer release];
- }
- else
- {
- [cell.cellView2.iconImageView setImage:[UIImage imageNamed:subInfo.iconString]];
- [cell.cellView2.niandaiLabel setText:subInfo.niandaiString];
- [cell.cellView2.titleLabel setText:subInfo.titleString];
- [cell.cellView2.contentLabel setText:subInfo.contentString];
- cell.cellView2.tag=8000+row*rowcellCount + i;
- UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellviewTaped:)];
- [ cell.cellView2 addGestureRecognizer:tapRecognizer];
- [tapRecognizer release];
- }
- }
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- return cell;
- }
- -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- // UITableViewCell *cell=[self tableView: tableView cellForRowAtIndexPath: indexPath];
- //
- //
- // return cell.frame.size.height;
- return 160;
- }
- - (void)dealloc
- {
- [super dealloc];
- [testTableView release];
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
效果如下图:
demo下载地址:http://download.csdn.net/detail/kuloveyouwei/6407621
我们知道在ios6以后,系统多了个UICollectionView可以显示多列数据,就像android的gridview,那么在ios6以前,我们如何实现呢?这里,我们通过一个tableview来实现。
它的原理是这样的,总共有多少条数据,除以要显示的列数,用来定义cell的行数,然后在一行中初始化这行中有多少列数据。。。。。。。
写个简单点的DEMO。。。结构如下:
这里,我们先定义一个头文件,InfoConfig.h,在这个头文件里我们定义相关的宏。。。
- #ifndef TestTableTwoRow_InfoConfig_h
- #define TestTableTwoRow_InfoConfig_h
- #define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
- #define rowcellCount 2
- #define RMarginX 0
- #define RMarginY 0
- #endif
然后我们定义一个数据模型SubCollectionsInfo类,这里只是封装了一些属性,提供set/get方法。。。。
- @interface SubCollectionsInfo : NSObject
- {
- NSString *_iconString;
- NSString *_niandaiString;
- NSString *_titleString;
- NSString *_contentString;
- NSString *_xmlFile;
- NSString *_idString;
- NSString *_nbidString;
- NSString *_timenmString;
- NSString *_stimeString;
- NSString *_etimeString;
- NSString *_zipFile;
- NSString *_md5String;
- }
- @property(nonatomic,copy) NSString *iconString;
- @property(nonatomic,copy) NSString *niandaiString;
- @property(nonatomic,copy) NSString *titleString;
- @property(nonatomic,copy) NSString *xmlFile;
- @property(nonatomic,copy) NSString *idString;
- @property(nonatomic,copy) NSString *contentString;
- @property(nonatomic,copy) NSString *nbidString;
- @property(nonatomic,copy) NSString *timenmString;
- @property(nonatomic,copy) NSString *stimeString;
- @property(nonatomic,copy) NSString *etimeString;
- @property(nonatomic,copy) NSString *zipFile;
- @property(nonatomic,copy) NSString *md5String;
- @property(nonatomic,assign) BOOL isselected;
- @property(nonatomic,assign) BOOL isdownload;
- @end
然后我们自定义一个tablecell继承于UITableViewCell,,,封装一个view,用来展示我们的界面
。。。。。具体可以看代码
最后,我们初始化一个tableview,加载相应的数据上去。。。
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- if (iPhone5) {
- self.view.frame=CGRectMake(0,0, 320, 568);
- }else
- {
- self.view.frame=CGRectMake(0,0, 320, 480);
- }
- [self.view setBackgroundColor:[UIColor whiteColor]];
- self.navigationItem.title=@"大帝";
- [self initData];
- [self initUI];
- }
- -(void) initData
- {
- dataArray=[[NSMutableArray alloc] init];
- for (int i=0; i<10; i++)
- {
- SubCollectionsInfo *info=[[SubCollectionsInfo alloc] init];
- info.niandaiString=@"王子大人";
- info.titleString=@"大帝";
- info.contentString=@"好人";
- info.iconString=@"celltouxiang.jpg";
- [dataArray addObject:info];
- [info release];
- }
- }
- -(void) initUI
- {
- testTableView=[[UITableView alloc] init];
- [testTableView setFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
- testTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
- [testTableView setBackgroundColor:[UIColor clearColor]];
- [self.view addSubview:testTableView];
- testTableView.dataSource=self;
- testTableView.delegate=self;
- }
- -(void)cellviewTaped:(UITapGestureRecognizer *)recognizer
- {
- int tag=[recognizer view].tag-8000;
- NSLog(@"%d",tag);
- }
- #pragma tableview
- -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return (dataArray.count-1)/rowcellCount+1;
- }
- -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier1 = @"Cell1";
- SubCollectionsCell2 *cell =(SubCollectionsCell2 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
- if (cell == nil)
- {
- cell = [[[SubCollectionsCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
- cell.backgroundColor=[UIColor clearColor];
- }
- NSUInteger row=[indexPath row];
- SubCollectionsInfo *subInfo=nil;
- for (NSInteger i = 0; i < rowcellCount; i++)
- {
- //奇数
- if (row*rowcellCount+i>dataArray.count-1)
- {
- break;
- }
- subInfo=[dataArray objectAtIndex:row*rowcellCount + i];
- if (i==0)
- {
- [cell.cellView1.iconImageView setImage:[UIImage imageNamed:subInfo.iconString]];
- [cell.cellView1.niandaiLabel setText:subInfo.niandaiString];
- [cell.cellView1.titleLabel setText:subInfo.titleString];
- [cell.cellView1.contentLabel setText:subInfo.contentString];
- cell.cellView1.tag=8000+row*rowcellCount + i;
- UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellviewTaped:)];
- [ cell.cellView1 addGestureRecognizer:tapRecognizer];
- [tapRecognizer release];
- }
- else
- {
- [cell.cellView2.iconImageView setImage:[UIImage imageNamed:subInfo.iconString]];
- [cell.cellView2.niandaiLabel setText:subInfo.niandaiString];
- [cell.cellView2.titleLabel setText:subInfo.titleString];
- [cell.cellView2.contentLabel setText:subInfo.contentString];
- cell.cellView2.tag=8000+row*rowcellCount + i;
- UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellviewTaped:)];
- [ cell.cellView2 addGestureRecognizer:tapRecognizer];
- [tapRecognizer release];
- }
- }
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- return cell;
- }
- -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- // UITableViewCell *cell=[self tableView: tableView cellForRowAtIndexPath: indexPath];
- //
- //
- // return cell.frame.size.height;
- return 160;
- }
- - (void)dealloc
- {
- [super dealloc];
- [testTableView release];
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
效果如下图:
demo下载地址:http://download.csdn.net/detail/kuloveyouwei/6407621