自己选择的路,摸爬滚打也要走下去

IOS开发笔记 多个tableView的使用

UITableView是app开发中常用到的控件,功能很强大,多用于数据的显示。下面以一个简单的实例来介绍tableview的基本用法。(适合新手,高手飘过)



  1. @interface TableViewTestViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{  
  2.   
  3.     UITableView *DataTable;  
  4.   
  5.     NSMutableArray *dataArray1; //定义数据数组1  
  6.   
  7.     NSMutableArray *dataArray2;//定义数据数组2  
  8.   
  9.     NSMutableArray *titleArray;//定义标题数组  
  10.   
  11. }  
  12.   
  13.    
  14.   
  15. - (void)viewDidLoad  
  16.   
  17. {  
  18.   
  19.     [superviewDidLoad];  
  20.   
  21. //初始化tableview  
  22.   
  23.     DataTable = [[UITableViewalloc] initWithFrame:CGRectMake(0, 0, 320, 420)];//指定位置大小  
  24.   
  25.     [DataTablesetDelegate:self];//指定委托  
  26.   
  27.     [DataTablesetDataSource:self];//指定数据委托  
  28.   
  29.     [self.viewaddSubview:DataTable];//加载tableview  
  30.   
  31.       
  32.   
  33.     dataArray1 = [[NSMutableArrayalloc] initWithObjects:@"中国", @"美国", @"英国", nil];//初始化数据数组1  
  34.   
  35.     dataArray2 = [[NSMutableArrayalloc] initWithObjects:@"黄种人", @"黑种人", @"白种人", nil];//初始化数据数组2  
  36.   
  37.     titleArray = [[NSMutableArrayalloc] initWithObjects:@"国家", @"种族", nil];//初始化标题数组  
  38.   
  39.       
  40.   
  41. }  
  42.   
  43.    
  44.   
  45. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  46.   
  47. {  
  48.   
  49.     // Return YES for supported orientations  
  50.   
  51.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  52.   
  53. }  
  54.   
  55.    
  56.   
  57.    
  58.   
  59. //每个section显示的标题  
  60.   
  61. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{  
  62.   
  63.     switch (section) {  
  64.   
  65.         case 0:  
  66.   
  67.             return [titleArray objectAtIndex:section];//提取标题数组的元素用来显示标题  
  68.   
  69.         case 1:  
  70.   
  71.             return [titleArray objectAtIndex:section];//提取标题数组的元素用来显示标题  
  72.   
  73.         default:  
  74.   
  75.             return @"Unknown";  
  76.   
  77.     }  
  78.   
  79.    
  80.   
  81. }  
  82.   
  83.    
  84.   
  85. //指定有多少个分区(Section),默认为1  
  86.   
  87. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
  88.   
  89.     return [titleArray count];//返回标题数组中元素的个数来确定分区的个数  
  90.   
  91. }  
  92.   
  93.    
  94.   
  95. //指定每个分区中有多少行,默认为1  
  96.   
  97. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  98.   
  99.     switch (section) {  
  100.   
  101.         case 0:  
  102.   
  103.            return  [dataArray1 count];//每个分区通常对应不同的数组,返回其元素个数来确定分区的行数  
  104.   
  105.             break;  
  106.   
  107.         case 1:  
  108.   
  109.             return  [dataArray2 count];  
  110.   
  111.             break;  
  112.   
  113.         default:  
  114.   
  115.             return 0;  
  116.   
  117.             break;  
  118.   
  119.     }  
  120.   
  121. }  
  122.   
  123.    
  124.   
  125. //绘制Cell  
  126.   
  127. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  128.   
  129.    
  130.   
  131.     static NSString *CellIdentifier = @"Cell";  
  132.   
  133.  //初始化cell并指定其类型,也可自定义cell  
  134.   
  135. UITableViewCell *cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:CellIdentifier];  
  136.   
  137.   if(cell == nil)   
  138.   
  139.   {  
  140.   
  141.   cell = [[[UITableViewCellalloc]   
  142.   
  143.   initWithFrame:CGRectZero   
  144.   
  145.   reuseIdentifier:CellIdentifier] autorelease];  
  146.   
  147. }  
  148.   
  149.    switch (indexPath.section) {  
  150.   
  151.   case 0://对应各自的分区  
  152.   
  153.     [[cell textLabel]  setText:[dataArray1 objectAtIndex:indexPath.row]];//给cell添加数据  
  154.   
  155.     break;  
  156.   
  157.   case 1:  
  158.   
  159.     [[cell textLabel]  setText:[dataArray2 objectAtIndex:indexPath.row]];  
  160.   
  161.     break;  
  162.   
  163.   default:  
  164.   
  165.     [[cell textLabel]  setText:@"Unknown"];  
  166.   
  167. }  
  168.   
  169.   return cell;//返回cell  
  170.   
  171. }  

上面的例子在功能上介绍了tableview的使用,但其在数据处理上具有很大的局限性。当我们要从服务器上请求数据,面对多种可能的数据(主要指数组的个数不稳定)此时上面的switch将无法满足我们的需求了。使用switch可是代码的结构清晰明了,但其局限性很致命(switch中case的个数无法动态指定),下面的另一种方法可解决上述问题。



代码在原由基础上进行的修改:

  1. @interface TableViewTestViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{  
  2.   
  3.     UITableView *DataTable;  
  4.   
  5.     NSMutableArray *dataArray1;  
  6.   
  7.     NSMutableArray *dataArray2;  
  8.   
  9.     NSMutableArray *titleArray;  
  10.   
  11.     NSMutableArray *dataArray; //加入了用于保存数组的数组 dataArray  
  12.   
  13. }  
  14.   
  15.    
  16.   
  17. - (void)viewDidLoad  
  18.   
  19. {  
  20.   
  21.     [superviewDidLoad];  
  22.   
  23.     DataTable = [[UITableViewalloc] initWithFrame:CGRectMake(0, 0, 320, 420)];  
  24.   
  25.     [DataTablesetDelegate:self];  
  26.   
  27.     [DataTablesetDataSource:self];  
  28.   
  29.     [self.viewaddSubview:DataTable];  
  30.   
  31.       
  32.   
  33.     dataArray1 = [[NSMutableArrayalloc] initWithObjects:@"中国", @"美国", @"英国", nil];  
  34.   
  35.     dataArray2 = [[NSMutableArrayalloc] initWithObjects:@"黄种人", @"黑种人", @"白种人", nil];  
  36.   
  37.     titleArray = [[NSMutableArrayalloc] initWithObjects:@"国家", @"种族", nil];  
  38.   
  39.     dataArray = [[NSMutableArrayalloc] initWithObjects:dataArray1, dataArray2, nil]; //初始化dataArray,元素为数组  
  40.   
  41.       
  42.   
  43. }  
  44.   
  45.    
  46.   
  47.    
  48.   
  49. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  50.   
  51. {  
  52.   
  53.     // Return YES for supported orientations  
  54.   
  55.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  56.   
  57. }  
  58.   
  59.  //制定个性标题,这里通过UIview来设计标题,功能上丰富,变化多。  
  60.   
  61. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {  
  62.   
  63.     UIView *view = [[[UIViewalloc] initWithFrame:CGRectMake(0, 0, 320, 40)] autorelease];  
  64.   
  65.     [view setBackgroundColor:[UIColorbrownColor]];//改变标题的颜色,也可用图片  
  66.   
  67.     UILabel *label = [[UILabelalloc] initWithFrame:CGRectMake(5, 5, 100, 30)];  
  68.   
  69.     label.textColor = [UIColorredColor];  
  70.   
  71.     label.backgroundColor = [UIColorclearColor];  
  72.   
  73.     label.text = [titleArrayobjectAtIndex:section];  
  74.   
  75.     [view addSubview:label];  
  76.   
  77.      return view;  
  78.   
  79. }  
  80.   
  81.  //指定标题的高度  
  82.   
  83. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{  
  84.   
  85.     return 40;  
  86.   
  87. }  
  88.   
  89.    
  90.   
  91. //每个section显示的标题,有了上面的这个就不要了  
  92.   
  93. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{  
  94.   
  95. }  
  96.   
  97.    
  98.   
  99. //指定有多少个分区(Section),默认为1  
  100.   
  101. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
  102.   
  103.     return [titleArraycount];  
  104.   
  105. }  
  106.   
  107.    
  108.   
  109. //指定每个分区中有多少行,默认为1  
  110.   
  111. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  112.   
  113.    /* switch (section) { 
  114.  
  115.         case 0: 
  116.  
  117.            return  [dataArray1 count]; 
  118.  
  119.             break; 
  120.  
  121.         case 1: 
  122.  
  123.             return  [dataArray2 count]; 
  124.  
  125.             break; 
  126.  
  127.         default: 
  128.  
  129.             return 0; 
  130.  
  131.             break; 
  132.  
  133.     }*/  
  134.   
  135.   /*  for(int i = 0; i < [titleArray count]; i++){ 
  136.  
  137.         if(section == i){ 
  138.  
  139.             return [[dataArray objectAtIndex:section] count]; 
  140.  
  141.         } 
  142.  
  143.     }*/  
  144.   
  145.   //上面的方法也是可行的,大家参考比较下  
  146.   
  147.     return [[dataArray objectAtIndex:section] count];  //取dataArray中的元素,并根据每个元素(数组)来判断分区中的行数。  
  148.   
  149.       
  150.   
  151.       
  152.   
  153. }  
  154.   
  155.    
  156.   
  157. //绘制Cell  
  158.   
  159. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  160.   
  161.    
  162.   
  163.     static NSString *CellIdentifier = @"Cell";  
  164.   
  165.    
  166.   
  167. UITableViewCell *cell = (UITableViewCell*)[tableView   
  168.   
  169.                                                dequeueReusableCellWithIdentifier:CellIdentifier];  
  170.   
  171. if(cell == nil)   
  172.   
  173. {  
  174.   
  175. cell = [[[UITableViewCellalloc]   
  176.   
  177. initWithFrame:CGRectZero   
  178.   
  179. reuseIdentifier:CellIdentifier] autorelease];  
  180.   
  181. }  
  182.   
  183.    
  184.   
  185. /*switch (indexPath.section) { 
  186.  
  187. case 0: 
  188.  
  189. [[cell textLabel]  
  190.  
  191. setText:[dataArray1 objectAtIndex:indexPath.row]]; 
  192.  
  193. break; 
  194.  
  195. case 1: 
  196.  
  197. [[cell textLabel]  
  198.  
  199. setText:[dataArray2 objectAtIndex:indexPath.row]]; 
  200.  
  201. break; 
  202.  
  203. default: 
  204.  
  205. [[cell textLabel]  
  206.  
  207. setText:@"Unknown"]; 
  208.  
  209. }*/  
  210.   
  211.     //上面的方法也可行,大家比较下。  
  212.   
  213.     [[cell textLabel] setText:[[dataArrayobjectAtIndex:indexPath.section]objectAtIndex:indexPath.row]];  
  214.   
  215.  //同上,取出dataArray中每个分区所对应的元素(数组),并通过其来取值。 (大家要有想像力, 复制代码试试就明白了)  
  216.   
  217.       
  218.   
  219. return cell;  
  220.   
  221.    
  222.   
  223. }  
  224.   
  225.    
  226.   
  227.  //改变行的高度  
  228.   
  229. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{  
  230.   
  231.     return40;  
  232.   
  233. }  

转自:http://www.cnblogs.com/top5/archive/2012/05/17/2506604.html




一天,一个月,一年。总有一天会变得不一样。

posted on 2014-08-20 10:44 wokaoJune 阅读(2683) 评论(0)  编辑  收藏 所属分类: IOS


只有注册用户登录后才能发表评论。


网站导航:
 
<2014年8月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

公告

GO ,GO,GO
自己选择的路,摸爬滚打也要走下去

常用链接

留言簿

随笔分类(26)

随笔档案(29)

文章分类

最新随笔

搜索

最新评论

阅读排行榜

评论排行榜