自己選擇的路,摸爬滾打也要走下去

          IOS開發(fā)筆記 多個(gè)tableView的使用

          UITableView是app開發(fā)中常用到的控件,功能很強(qiáng)大,多用于數(shù)據(jù)的顯示。下面以一個(gè)簡(jiǎn)單的實(shí)例來介紹tableview的基本用法。(適合新手,高手飄過)



          1. @interface TableViewTestViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{  
          2.   
          3.     UITableView *DataTable;  
          4.   
          5.     NSMutableArray *dataArray1; //定義數(shù)據(jù)數(shù)組1  
          6.   
          7.     NSMutableArray *dataArray2;//定義數(shù)據(jù)數(shù)組2  
          8.   
          9.     NSMutableArray *titleArray;//定義標(biāo)題數(shù)組  
          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];//指定數(shù)據(jù)委托  
          28.   
          29.     [self.viewaddSubview:DataTable];//加載tableview  
          30.   
          31.       
          32.   
          33.     dataArray1 = [[NSMutableArrayalloc] initWithObjects:@"中國", @"美國", @"英國", nil];//初始化數(shù)據(jù)數(shù)組1  
          34.   
          35.     dataArray2 = [[NSMutableArrayalloc] initWithObjects:@"黃種人", @"黑種人", @"白種人", nil];//初始化數(shù)據(jù)數(shù)組2  
          36.   
          37.     titleArray = [[NSMutableArrayalloc] initWithObjects:@"國家", @"種族", nil];//初始化標(biāo)題數(shù)組  
          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. //每個(gè)section顯示的標(biāo)題  
          60.   
          61. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{  
          62.   
          63.     switch (section) {  
          64.   
          65.         case 0:  
          66.   
          67.             return [titleArray objectAtIndex:section];//提取標(biāo)題數(shù)組的元素用來顯示標(biāo)題  
          68.   
          69.         case 1:  
          70.   
          71.             return [titleArray objectAtIndex:section];//提取標(biāo)題數(shù)組的元素用來顯示標(biāo)題  
          72.   
          73.         default:  
          74.   
          75.             return @"Unknown";  
          76.   
          77.     }  
          78.   
          79.    
          80.   
          81. }  
          82.   
          83.    
          84.   
          85. //指定有多少個(gè)分區(qū)(Section),默認(rèn)為1  
          86.   
          87. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
          88.   
          89.     return [titleArray count];//返回標(biāo)題數(shù)組中元素的個(gè)數(shù)來確定分區(qū)的個(gè)數(shù)  
          90.   
          91. }  
          92.   
          93.    
          94.   
          95. //指定每個(gè)分區(qū)中有多少行,默認(rèn)為1  
          96.   
          97. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
          98.   
          99.     switch (section) {  
          100.   
          101.         case 0:  
          102.   
          103.            return  [dataArray1 count];//每個(gè)分區(qū)通常對(duì)應(yīng)不同的數(shù)組,返回其元素個(gè)數(shù)來確定分區(qū)的行數(shù)  
          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://對(duì)應(yīng)各自的分區(qū)  
          152.   
          153.     [[cell textLabel]  setText:[dataArray1 objectAtIndex:indexPath.row]];//給cell添加數(shù)據(jù)  
          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的使用,但其在數(shù)據(jù)處理上具有很大的局限性。當(dāng)我們要從服務(wù)器上請(qǐng)求數(shù)據(jù),面對(duì)多種可能的數(shù)據(jù)(主要指數(shù)組的個(gè)數(shù)不穩(wěn)定)此時(shí)上面的switch將無法滿足我們的需求了。使用switch可是代碼的結(jié)構(gòu)清晰明了,但其局限性很致命(switch中case的個(gè)數(shù)無法動(dòng)態(tài)指定),下面的另一種方法可解決上述問題。



          代碼在原由基礎(chǔ)上進(jìn)行的修改:

          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; //加入了用于保存數(shù)組的數(shù)組 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,元素為數(shù)組  
          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.  //制定個(gè)性標(biāo)題,這里通過UIview來設(shè)計(jì)標(biāo)題,功能上豐富,變化多。  
          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]];//改變標(biāo)題的顏色,也可用圖片  
          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.  //指定標(biāo)題的高度  
          82.   
          83. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{  
          84.   
          85.     return 40;  
          86.   
          87. }  
          88.   
          89.    
          90.   
          91. //每個(gè)section顯示的標(biāo)題,有了上面的這個(gè)就不要了  
          92.   
          93. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{  
          94.   
          95. }  
          96.   
          97.    
          98.   
          99. //指定有多少個(gè)分區(qū)(Section),默認(rèn)為1  
          100.   
          101. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
          102.   
          103.     return [titleArraycount];  
          104.   
          105. }  
          106.   
          107.    
          108.   
          109. //指定每個(gè)分區(qū)中有多少行,默認(rèn)為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中的元素,并根據(jù)每個(gè)元素(數(shù)組)來判斷分區(qū)中的行數(shù)。  
          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中每個(gè)分區(qū)所對(duì)應(yīng)的元素(數(shù)組),并通過其來取值。 (大家要有想像力, 復(fù)制代碼試試就明白了)  
          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. }  

          轉(zhuǎn)自:http://www.cnblogs.com/top5/archive/2012/05/17/2506604.html




          一天,一個(gè)月,一年??傆幸惶鞎?huì)變得不一樣。

          posted on 2014-08-20 10:44 wokaoJune 閱讀(2705) 評(píng)論(0)  編輯  收藏 所屬分類: IOS


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2014年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導(dǎo)航

          統(tǒng)計(jì)

          公告

          GO ,GO,GO
          自己選擇的路,摸爬滾打也要走下去

          常用鏈接

          留言簿

          隨筆分類(26)

          隨筆檔案(29)

          文章分類

          最新隨筆

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 齐河县| 仙游县| 江川县| 栾城县| 垣曲县| 通山县| 富阳市| 德昌县| 柏乡县| 肇州县| 海安县| 济宁市| 合水县| 道真| 银川市| 邮箱| 峨边| 九龙坡区| 鄄城县| 樟树市| 馆陶县| 个旧市| 驻马店市| 寿宁县| 元江| 潢川县| 宁河县| 玉龙| 桐城市| 曲麻莱县| 中牟县| 江华| 寻乌县| 小金县| 昌平区| 威宁| 枣庄市| 东至县| 绥棱县| 武夷山市| 清远市|