ios中uitableview显示两列或多列数据
2013-10-16 14:43
162 查看
我们知道在ios6以后,系统多了个UICollectionView可以显示多列数据,就像android的gridview,那么在ios6以前,我们如何实现呢?这里,我们通过一个tableview来实现。
它的原理是这样的,总共有多少条数据,除以要显示的列数,用来定义cell的行数,然后在一行中初始化这行中有多少列数据。。。。。。。
写个简单点的DEMO。。。结构如下:
![](http://img.blog.csdn.net/20131016143302171?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva3Vsb3ZleW91d2Vp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
这里,我们先定义一个头文件,InfoConfig.h,在这个头文件里我们定义相关的宏。。。
然后我们定义一个数据模型SubCollectionsInfo类,这里只是封装了一些属性,提供set/get方法。。。。
然后我们自定义一个tablecell继承于UITableViewCell,,,封装一个view,用来展示我们的界面
。。。。。具体可以看代码
最后,我们初始化一个tableview,加载相应的数据上去。。。
效果如下图:
![](http://img.blog.csdn.net/20131016143852796?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva3Vsb3ZleW91d2Vp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
demo下载地址:http://download.csdn.net/detail/kuloveyouwei/6407621
它的原理是这样的,总共有多少条数据,除以要显示的列数,用来定义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
相关文章推荐
- ios中uitableview显示两列或多列数据
- ios中uitableview显示两列或多列数据
- ios中uitableview显示两列或多列数据
- IOS第七天(3:UiTableView 模型和数据的分组的显示)
- IOS开发学习笔记028-UITableView单组数据显示代码优化
- iOS笔记:查询结果为空,显示无数据的方法(UITableView Display No Data)
- IOS第七天(4:UiTableView 数据的显示优化重复实例和tableFooterView和tableHeaderView)
- IOS开发- UITableView 无数据时,显示“暂无数据”背景的实现
- iOS开发>>>UITableview实现更多数据显示方法
- 使用 UITableView 创建表格应用——从plist文件加载并显示数据(转)
- 在iOS中UITableView控件不显示能容
- 01-UI基础-04-02-UITableView多组数据显示
- IOS开发(27)之UITableView的Cell显示长按快捷菜单
- [ios开发技巧之]UIPickerView的数据循环显示
- iOS-分组UITableView删除崩溃问题(当删除section中最后一条数据崩溃的情况)
- iOS常见问题之异步加载图片在UITableView里显示时的奇异的现象
- iOS UITableView数据刷新
- IOS 构造和使用TableView(基于storyboard) (一)基本的数据显示
- iOS 如何让UITableView显示时自动滚动到底部(不闪动)
- iOS开发UI篇—实现UItableview控件数据刷新