ios中uitableview显示两列或多列数据

转自:http://blog.csdn.net/kuloveyouwei/article/details/12780191

我们知道在ios6以后,系统多了个UICollectionView可以显示多列数据,就像android的gridview,那么在ios6以前,我们如何实现呢?这里,我们通过一个tableview来实现。

它的原理是这样的,总共有多少条数据,除以要显示的列数,用来定义cell的行数,然后在一行中初始化这行中有多少列数据。。。。。。。

写个简单点的DEMO。。。结构如下:


这里,我们先定义一个头文件,InfoConfig.h,在这个头文件里我们定义相关的宏。。。

[objc]  view plain copy
  1. #ifndef TestTableTwoRow_InfoConfig_h  
  2. #define TestTableTwoRow_InfoConfig_h  
  3.   
  4.   
  5.   
  6. #define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)  
  7.   
  8. #define rowcellCount 2  
  9. #define RMarginX 0  
  10. #define RMarginY 0  
  11.   
  12. #endif  

然后我们定义一个数据模型SubCollectionsInfo类,这里只是封装了一些属性,提供set/get方法。。。。

[objc]  view plain copy
  1. @interface SubCollectionsInfo : NSObject  
  2. {  
  3.   
  4.       
  5.     NSString *_iconString;  
  6.     NSString *_niandaiString;  
  7.     NSString *_titleString;  
  8.     NSString *_contentString;  
  9.     NSString *_xmlFile;  
  10.     NSString *_idString;  
  11.     NSString *_nbidString;  
  12.     NSString *_timenmString;  
  13.     NSString *_stimeString;  
  14.     NSString *_etimeString;  
  15.     NSString *_zipFile;  
  16.     NSString *_md5String;  
  17.   
  18. }  
  19.   
  20.   
  21. @property(nonatomic,copyNSString *iconString;  
  22. @property(nonatomic,copyNSString *niandaiString;  
  23. @property(nonatomic,copyNSString *titleString;  
  24. @property(nonatomic,copyNSString *xmlFile;  
  25. @property(nonatomic,copyNSString *idString;  
  26. @property(nonatomic,copyNSString *contentString;  
  27. @property(nonatomic,copyNSString *nbidString;  
  28. @property(nonatomic,copyNSString *timenmString;  
  29. @property(nonatomic,copyNSString *stimeString;  
  30. @property(nonatomic,copyNSString *etimeString;  
  31. @property(nonatomic,copyNSString *zipFile;  
  32. @property(nonatomic,copyNSString *md5String;  
  33.   
  34. @property(nonatomic,assign) BOOL isselected;  
  35. @property(nonatomic,assign) BOOL isdownload;  
  36.   
  37. @end  

然后我们自定义一个tablecell继承于UITableViewCell,,,封装一个view,用来展示我们的界面

。。。。。具体可以看代码

最后,我们初始化一个tableview,加载相应的数据上去。。。

[objc]  view plain copy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.       
  5.       
  6.     if (iPhone5) {  
  7.           
  8.         self.view.frame=CGRectMake(0,0320568);  
  9.           
  10.     }else  
  11.     {  
  12.           
  13.         self.view.frame=CGRectMake(0,0320480);  
  14.           
  15.     }  
  16.       
  17.     [self.view setBackgroundColor:[UIColor whiteColor]];  
  18.       
  19.       
  20.     self.navigationItem.title=@"大帝";  
  21.       
  22.     [self initData];  
  23.     [self initUI];  
  24.   
  25.       
  26. }  
  27.   
  28. -(void) initData  
  29. {  
  30.   
  31.   
  32.     dataArray=[[NSMutableArray alloc] init];  
  33.       
  34.     for (int i=0; i<10; i++)  
  35.     {  
  36.           
  37.         SubCollectionsInfo *info=[[SubCollectionsInfo alloc] init];  
  38.           
  39.         info.niandaiString=@"王子大人";  
  40.         info.titleString=@"大帝";  
  41.         info.contentString=@"好人";  
  42.         info.iconString=@"celltouxiang.jpg";  
  43.           
  44.         [dataArray addObject:info];  
  45.           
  46.         [info release];  
  47.           
  48.     }  
  49.   
  50.   
  51. }  
  52.   
  53.   
  54. -(void) initUI  
  55. {  
  56.   
  57.   
  58.     testTableView=[[UITableView alloc] init];  
  59.     [testTableView setFrame:CGRectMake(00self.view.frame.size.width,self.view.frame.size.height)];  
  60.     testTableView.separatorStyle=UITableViewCellSeparatorStyleNone;  
  61.     [testTableView setBackgroundColor:[UIColor clearColor]];  
  62.       
  63.     [self.view addSubview:testTableView];  
  64.   
  65.   
  66.     testTableView.dataSource=self;  
  67.     testTableView.delegate=self;  
  68.   
  69.   
  70.       
  71. }  
  72.   
  73. -(void)cellviewTaped:(UITapGestureRecognizer *)recognizer  
  74. {  
  75.       
  76.     int tag=[recognizer view].tag-8000;  
  77.       
  78.     NSLog(@"%d",tag);  
  79.       
  80.      
  81.       
  82. }  
  83.   
  84.   
  85. #pragma tableview  
  86. -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  87. {  
  88.       
  89.       
  90.     return (dataArray.count-1)/rowcellCount+1;  
  91.       
  92.       
  93. }  
  94.   
  95. -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  96. {  
  97.       
  98.     static NSString *CellIdentifier1 = @"Cell1";  
  99.       
  100.       
  101.     SubCollectionsCell2 *cell =(SubCollectionsCell2 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];  
  102.       
  103.     if (cell == nil)  
  104.     {  
  105.           
  106.         cell = [[[SubCollectionsCell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];  
  107.           
  108.         cell.backgroundColor=[UIColor clearColor];  
  109.     }  
  110.       
  111.       
  112.     NSUInteger row=[indexPath row];  
  113.       
  114.       
  115.     SubCollectionsInfo *subInfo=nil;  
  116.       
  117.     for (NSInteger i = 0; i < rowcellCount; i++)  
  118.     {  
  119.           
  120.         //奇数  
  121.         if (row*rowcellCount+i>dataArray.count-1)  
  122.         {  
  123.               
  124.             break;  
  125.               
  126.               
  127.         }  
  128.           
  129.         subInfo=[dataArray objectAtIndex:row*rowcellCount + i];  
  130.           
  131.         if (i==0)  
  132.         {  
  133.               
  134.             [cell.cellView1.iconImageView setImage:[UIImage imageNamed:subInfo.iconString]];  
  135.             [cell.cellView1.niandaiLabel setText:subInfo.niandaiString];  
  136.             [cell.cellView1.titleLabel setText:subInfo.titleString];  
  137.             [cell.cellView1.contentLabel setText:subInfo.contentString];  
  138.             cell.cellView1.tag=8000+row*rowcellCount + i;  
  139.               
  140.             UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellviewTaped:)];  
  141.             [ cell.cellView1 addGestureRecognizer:tapRecognizer];  
  142.             [tapRecognizer release];  
  143.               
  144.         }  
  145.         else  
  146.         {  
  147.               
  148.             [cell.cellView2.iconImageView setImage:[UIImage imageNamed:subInfo.iconString]];  
  149.             [cell.cellView2.niandaiLabel setText:subInfo.niandaiString];  
  150.             [cell.cellView2.titleLabel setText:subInfo.titleString];  
  151.             [cell.cellView2.contentLabel setText:subInfo.contentString];  
  152.             cell.cellView2.tag=8000+row*rowcellCount + i;  
  153.               
  154.             UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellviewTaped:)];  
  155.             [ cell.cellView2 addGestureRecognizer:tapRecognizer];  
  156.             [tapRecognizer release];  
  157.               
  158.               
  159.         }  
  160.           
  161.           
  162.     }  
  163.   
  164.       
  165.     cell.selectionStyle = UITableViewCellSelectionStyleNone;  
  166.       
  167.       
  168.     return cell;  
  169.       
  170. }  
  171.   
  172.   
  173. -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
  174. {  
  175.       
  176.     //    UITableViewCell *cell=[self tableView: tableView cellForRowAtIndexPath: indexPath];  
  177.     //  
  178.     //  
  179.     //    return cell.frame.size.height;  
  180.       
  181.       
  182.     return 160;  
  183.       
  184.       
  185. }  
  186.   
  187.   
  188.   
  189. - (void)dealloc  
  190. {  
  191.       
  192.     [super dealloc];  
  193.       
  194.       
  195.     [testTableView release];  
  196.       
  197. }  
  198.   
  199.   
  200.   
  201. - (void)didReceiveMemoryWarning  
  202. {  
  203.     [super didReceiveMemoryWarning];  
  204.     // Dispose of any resources that can be recreated.  
  205. }  
  206.   
  207. @end  

效果如下图:




demo下载地址:http://download.csdn.net/detail/kuloveyouwei/6407621


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值