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

          2014年6月27日

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

          UITableView是app開發(fā)中常用到的控件,功能很強(qiáng)大,多用于數(shù)據(jù)的顯示。下面以一個(gè)簡單的實(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


          posted @ 2014-08-20 10:44 wokaoJune 閱讀(313) | 評(píng)論 (0)編輯 收藏

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

          UITableView是app開發(fā)中常用到的控件,功能很強(qiáng)大,多用于數(shù)據(jù)的顯示。下面以一個(gè)簡單的實(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


          posted @ 2014-08-20 10:44 wokaoJune 閱讀(2707) | 評(píng)論 (0)編輯 收藏

          三十而立,從零開始學(xué)ios開發(fā)(十):Multiview Applications(多個(gè)xib之前的切換)

          這篇學(xué)習(xí)的主要內(nèi)容是Multiview,在我們學(xué)習(xí)iphone旋轉(zhuǎn)的時(shí)候,介紹過多個(gè)view的使用方法,不過這里的view和旋轉(zhuǎn)屏幕中所指的多個(gè)view是不同的,旋轉(zhuǎn)屏幕中涉及到的多個(gè)view是在一個(gè)xib文件中的,而我們這里所指的mulitview,則是指多個(gè)xib,在多個(gè)xib中進(jìn)行view的切換,也就是從一個(gè)xib切換到另一個(gè)xib,而每個(gè)xib中只有一個(gè)view。

          另外的一個(gè)不同點(diǎn)體現(xiàn)在創(chuàng)建項(xiàng)目的時(shí)候,到目前為止,我們創(chuàng)建的所有項(xiàng)目的template都是single view,這次創(chuàng)建的項(xiàng)目將使用新的template。

          Multiview applicatin的基本架構(gòu),一般來說,一個(gè)multiview application會(huì)有一個(gè)主要的controller來控制view的呈現(xiàn),這個(gè)主要的controller可以是toolbar(iphone上的Safari打開后,地下的一排按鈕就是toolbar)或者是tab bar(iphone打開phone,最下面一個(gè)一個(gè)的tab),或者其他的一些控件,他們控制到底那個(gè)view應(yīng)該顯示,那個(gè)view應(yīng)該被隱藏,也就是說,至少需要3個(gè)view才能實(shí)現(xiàn)view的切換,一個(gè)主要的controller view,2個(gè)其他的用于切換的view,主要的controller view是一直顯示在屏幕上的,而其他的view中,只有1個(gè)會(huì)顯示出來,其他的都被隱藏。ok,下面開始一步一步實(shí)現(xiàn)multiview吧,再說下去會(huì)越來越糊涂的。

          0)項(xiàng)目簡介
          今天要做的例子中包含3個(gè)view,一個(gè)controller view,我們會(huì)使用toolbar,2個(gè)用于切換的view,一個(gè)藍(lán)色底,一個(gè)黃色底,他們中間都有一個(gè)button,單擊button會(huì)有一個(gè)警告框彈出,告訴用戶當(dāng)前顯示的是哪個(gè)view。

          1)創(chuàng)建一個(gè)工程,選擇Empty Application

          這次不再選擇Single View Application,而選擇Empty Application,項(xiàng)目中的所有文件我們都會(huì)手動(dòng)進(jìn)行添加。

          單擊Next按鈕,之后的操作和創(chuàng)建Single View項(xiàng)目一樣,設(shè)定項(xiàng)目名稱“View Switcher”,設(shè)定項(xiàng)目保存路徑,項(xiàng)目創(chuàng)建完成。

          2)添加View Controller
          由于我們使用的模板是Empty Application,因此當(dāng)創(chuàng)建完項(xiàng)目后,只有以下一些文件

          里面并沒有我們需要的controller view,也沒有任何xib文件,這些都是需要我們手動(dòng)添加的。使用快捷鍵command+N或者菜單欄File>New>New File...,在彈出的窗口中,左邊選擇Cocoa Touch,右邊選擇UIViewController subclass,點(diǎn)擊Next按鈕

          填寫類名BIDSwitchViewController,其他都是默認(rèn)選項(xiàng)(注意最后一個(gè)checkbox,如果選擇了,則將創(chuàng)建一個(gè)和BIDSwitchViewController關(guān)聯(lián)的xib文件,我們?cè)谶@里可以選上,但是為了弄清楚view controller和xib文件是如何關(guān)聯(lián)在一起的,在這個(gè)項(xiàng)目中我們暫時(shí)不選,后面我們會(huì)手動(dòng)連接這兩個(gè)文件),點(diǎn)擊Next按鈕。

          選擇保持的位置,保存在“View Switcher”目錄下,完成創(chuàng)建。

          BIDSwitchViewController是項(xiàng)目的最頂層的view controller(root controller),它用于控制另外2個(gè)view的切換,下面按照同樣的方法,創(chuàng)建另外2個(gè)view controller,一個(gè)名字教BIDBlueViewController,另一個(gè)叫做BIDYellowViewController,他們都不需要關(guān)聯(lián)xib,且都保存在“View Switcher”目錄下。創(chuàng)建完成后的“View Switcher”結(jié)構(gòu)如下

          3)添加xib文件
          使用快捷鍵command+N或者菜單欄File>New>New File...,在彈出的窗口中,左邊選擇User Interface,右邊選擇View,點(diǎn)擊Next按鈕

          Device Family中選擇iphone,點(diǎn)擊Next

          命名為SwitchView.xib,同樣保持在“View Switcher”目錄下,點(diǎn)擊Create,完成創(chuàng)建。

          使用同樣的方法創(chuàng)建另外兩個(gè)xib,分別命名為BlueView.xib和YellowView.xib。至此,我們所有的文件都已經(jīng)創(chuàng)建完畢,整個(gè)的“View Switcher”結(jié)構(gòu)圖如下

          接下來就是寫代碼的工作了。

          4)編輯BIDAppDelegate文件
          當(dāng)一個(gè)app啟動(dòng)的時(shí)候,我們都會(huì)默認(rèn)的把一個(gè)view載入當(dāng)前的iphone窗口(application's main window),在這個(gè)例子中,這個(gè)view就是我們的root view,即BIDSwitchViewController。我們是在BIDAppDelegate文件中設(shè)置默認(rèn)載入的view的,因此首先打開BIDAppDelegate.h,添加class BIDSwitchViewController,和它的一個(gè)property,如下

          復(fù)制代碼
          #import <UIKit/UIKit.h> @class BIDSwitchViewController; @interface BIDAppDelegate : UIResponder <UIApplicationDelegate>  @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) BIDSwitchViewController *switchViewController;  @end
          復(fù)制代碼

          其中,@class是告訴BIDAppDelegate,后面的BIDSwitchViewController是一個(gè)類,應(yīng)該以類的方式處理該對(duì)象,后面在聲明property的時(shí)候,BIDAppDelegate就知道BIDSwitchViewController是一個(gè)類,不會(huì)不認(rèn)該對(duì)象。

          接著打開BIDAppDelegate.m,添加如下代碼

          復(fù)制代碼
          #import "BIDAppDelegate.h" #import "BIDSwitchViewController.h"  @implementation BIDAppDelegate  @synthesize window = _window; @synthesize switchViewController;  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];     // Override point for customization after application launch.          self.switchViewController = [[BIDSwitchViewController alloc] initWithNibName:@"SwitchView" bundle:nil];     UIView *switchView = self.switchViewController.view;     CGRect switchViewFrame = switchView.frame;     switchViewFrame.origin.y += [UIApplication sharedApplication].statusBarFrame.size.height;     switchView.frame = switchViewFrame;     [self.window addSubview:switchView];          self.window.backgroundColor = [UIColor whiteColor];     [self.window makeKeyAndVisible];     return YES; }  ......
          復(fù)制代碼

          首先import BIDSwitchViewController,然后聲明synthesize,對(duì)應(yīng)于頭文件中的property。

          didFinishLaunchingWithOption方法,當(dāng)一個(gè)app載入完成后需要做一些什么事情,在這里,我們指定了哪個(gè)view被載入到windows中作為默認(rèn)顯示的view。

          self.switchViewController = [[BIDSwitchViewControlleralloc] initWithNibName:@"SwitchView" bundle:nil];
          通過xib(在舊的版本中,xib被稱為nib,因此這里出現(xiàn)的是NibName)的名字來制定初始化哪個(gè)view

          UIView *switchView = self.switchViewController.view;
          獲取view

          CGRect switchViewFrame = switchView.frame;
          得到view的frame,也就是這個(gè)view的顯示位置,在前幾篇的文章中提到過這個(gè)屬性。

          switchViewFrame.origin.y += [UIApplicationsharedApplication].statusBarFrame.size.height;
          我覺得這句話比較重要,它將view的位置往下移動(dòng)了20point(point在非retina屏幕中是20px,在retina屏幕中是40px),這樣就不會(huì)擋住iphone頂部的狀態(tài)欄了。

          switchView.frame = switchViewFrame;
          將修改過的frame從新賦值給switchView

          [self.window addSubview:switchView];
          將switchView設(shè)置成window的subview。怎么理解這句話呢,就是說,一個(gè)app只有一個(gè)窗口(window),這個(gè)window只能同時(shí)顯示一個(gè)view,且這個(gè)view是基于這個(gè)window而存在的,是放在這個(gè)window里面的,因此稱之為window的subview,子視圖。

          5)編輯BIDSwitchViewController.h
          BIDSwitchViewController是root controller,用于控制其他2個(gè)view的切換,因此需要在其頭文件中聲明其他兩個(gè)controller,然后需要定義一個(gè)Action,用來完成對(duì)2個(gè)view的切換,將BIDSwitchViewController.h修改成如下

          復(fù)制代碼
          #import <UIKit/UIKit.h>  @class BIDBlueViewController; @class BIDYellowViewController;  @interface BIDSwitchViewController : UIViewController  @property (strong, nonatomic) BIDBlueViewController *blueViewController; @property (strong, nonatomic) BIDYellowViewController *yellowViewController;  - (IBAction)switchViews:(id)sender;  @end
          復(fù)制代碼

          代碼還是很好理解的,和前面在BIDAppDelegate.h中添加BIDSwitchViewController是一樣的。

          6)關(guān)聯(lián)BIDSwitchViewController和SwitchView.xib
          在Project Navigator中選中SwitchView.xib,在xib的dock中選中File's Owner

          然后在Identity inspector中將Class改成BIDSwitchViewController

          這樣就將SwitchView.xib關(guān)聯(lián)到了BIDSwitchViewController,如果我們選擇Connections inspector,會(huì)看到我們剛才在BIDSwitchViewController.h中定義的Action:switchViews出現(xiàn)在Received Actions,我們之后就可以將這個(gè)Action關(guān)聯(lián)到SwitchView.xib的控件上。

          7)在SwitchView.xib上添加Toolbar
          在這個(gè)例子總,我們切換veiw的方式是點(diǎn)擊Toolbar上的一個(gè)button,然后切換2個(gè)view,SwitchView.xib使我們的root view,因此我們需要在SwitchView.xib上添加一個(gè)toolbar,然后點(diǎn)擊toolbar上的按鈕,切換BlueView.xib和YellowView.xib。

          選中SwitchView.xib,在object library中找到Toolbar

          拖動(dòng)到View上,放在最下方

          默認(rèn)的,已經(jīng)有一個(gè)button在Toolbar上了,雙擊button改變文字,將文字改成“Switch Views”

          接著就是將“Switch Views”按鈕關(guān)聯(lián)到switchViews,選中“Switch Views”,control-dragged到File's Owner,在彈出的框中選中switchViews

          打開Connections inspector,我們可以看到關(guān)聯(lián)后的情況

          有一個(gè)不同的地方,Toolbar上的button不像一般的button,會(huì)有很多的方法讓你進(jìn)行關(guān)聯(lián),Toolbar上button的Sent Actions(其他的button叫做Send Events,有很多個(gè)方法)只有一個(gè)方法,而它的作用相當(dāng)于一般button的touch up inside。

          8)關(guān)聯(lián)SwitchView.xib和BIDSwitchViewController's view outlet
          BIDSwitchViewController繼承自UIViewController,在UIViewController中有一個(gè)outlet view,另外當(dāng)我們?cè)谧龅?)步的時(shí)候,將SwitchView.xib的class改成了BIDSwitchViewController,所以我們要將這個(gè)view關(guān)聯(lián)到SwitchView.xib,關(guān)聯(lián)的方法是選中SwitchView.xib,然后選中File's Owner,control-drag到下面的View

          釋放鼠標(biāo)后,在填出的框中選則view,這樣就關(guān)聯(lián)好了。

          關(guān)聯(lián)好后,查看Connections inspector,也可以看到關(guān)聯(lián)后的結(jié)果

          9)編輯BIDSwitchViewController.m
          添加如下代碼

          復(fù)制代碼
          #import "BIDSwitchViewController.h" #import "BIDYellowViewController.h" #import "BIDBlueViewController.h"  @implementation BIDSwitchViewController @synthesize yellowViewController; @synthesize blueViewController;
          復(fù)制代碼

          2個(gè)#import這個(gè)很好理解,因?yàn)锽IDSwitchViewController是root controller,會(huì)控制另外2個(gè)controller,因此需要把另外2個(gè)controller引入進(jìn)來,這樣才可以對(duì)他們進(jìn)行操作。
          2個(gè)synthesize對(duì)著頭文件中的2個(gè)property。

          復(fù)制代碼
          // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad {     self.blueViewController = [[BIDBlueViewController alloc] initWithNibName:@"BlueView" bundle:nil];     [self.view insertSubview:self.blueViewController.view atIndex:0];     [super viewDidLoad]; }
          復(fù)制代碼

          首先去掉viewDidLoad的注釋,然后添加以上的代碼。從最后一句可以看出,viewDidLoad方法繼承自UIViewController,在這里對(duì)其重載,這個(gè)方法發(fā)生在當(dāng)view已經(jīng)載入完成后,我們所要做的是當(dāng)root view載入完成后,在載入root view的subview,在我們的這個(gè)例子中是BlueView。BlueView的載入方法和前面的一樣,使用initWithNibName,然后作為subView插入到當(dāng)前的view中(當(dāng)前的view就是root view,也就是SwitchView)。

          復(fù)制代碼
          - (IBAction)switchViews:(id)sender {     if(self.yellowViewController.view.superview == nil) {         if(self.yellowViewController == nil) {             self.yellowViewController = [[BIDYellowViewController alloc] initWithNibName:@"YellowView" bundle:nil];         }         [blueViewController.view removeFromSuperview];         [self.view insertSubview:self.yellowViewController.view atIndex:0];     } else {         if (self.blueViewController ==nil) {             self.blueViewController = [[BIDBlueViewController alloc] initWithNibName:@"BlueView" bundle:nil];         }         [yellowViewController.view removeFromSuperview];         [self.view insertSubview:self.blueViewController.view atIndex:0];     } }
          復(fù)制代碼

          實(shí)現(xiàn)switchViews Action,上面的代碼還是很好理解的,首先判斷當(dāng)前哪個(gè)subview是沒有superview的,因?yàn)檫@2個(gè)subview不會(huì)同時(shí)顯示,當(dāng)blueSubview顯示時(shí),YellowSubview就會(huì)從root view中移除,因此不會(huì)具有superview,當(dāng)?shù)弥莻€(gè)subview沒有superview,就說明應(yīng)該顯示這個(gè)subview。知道該顯示哪個(gè)subview后,再判斷這個(gè)subview是否還存在(是否需要重新載入初始化),然后將另一個(gè)subview從superview中移除,再將subview顯示出來。

          復(fù)制代碼
          - (void)didReceiveMemoryWarning {     // Releases the view if it doesn't have a superview.     [super didReceiveMemoryWarning];          // Release any cached data, images, etc that aren't in use.     if (self.blueViewController.view.superview == nil){         self.blueViewController = nil;     } else {         self.yellowViewController = nil;     } }
          復(fù)制代碼

          當(dāng)ios的內(nèi)存不夠用時(shí),didReceiveMemoryWarning會(huì)被系統(tǒng)自動(dòng)調(diào)用,來釋放可利用的內(nèi)存。在這里如果哪個(gè)subview沒有顯示,就釋放該subview,騰出內(nèi)存。

          至此BIDSwitchViewController的所有代碼都寫好了,下面就應(yīng)該處理BIDBlueVeiwController和BIDYellowViewController了。

          (友情提示,時(shí)不時(shí)的編譯一下你的project,盡早發(fā)現(xiàn)問題,容易修改,否則到后面自己都不知道錯(cuò)在哪里)

          10)編輯BlueView.xib和YellowView.xib
          分別在BlueView.xib和YellowView.xib上添加一個(gè)button,并命名為“Press me”。

          修改BlueView.xib的class為BIDBlueViewController,修改YellowView.xib的class為BIDYellowController(修改方法:選中xib,點(diǎn)擊File's Owner,在Identity inspector中更改class)
           
          class改變了,就需要重新關(guān)聯(lián)一下File's owner的view,方法和之前的一樣,選中File‘s Owner,control-drag到下面的View上,在彈出的框中選擇view,關(guān)聯(lián)完畢,2個(gè)xib都需要進(jìn)行這個(gè)操作。

          在Attributes inspector中,將BlueView.xib的background顏色改成藍(lán)色

          同樣的方法將YellowView.xib的background顏色改成黃色

          還有一個(gè)地方需要設(shè)置,因?yàn)槲覀兪窃趓oot view中顯示這2個(gè)subview,而root view有一個(gè)toolbar在最下方,因此subview需要把toolbar的位置空出來,再進(jìn)行自己的布局,還是打開Attributes inspector,在Simulated Metrics欄中有很多現(xiàn)象,他們都是用來模擬iphone屏幕上各種占位控件的,我們將Button Bar的選項(xiàng)設(shè)成Toolbar,這樣xib就會(huì)空出Toolbar的位置來進(jìn)行布局計(jì)算了

          仔細(xì)看的話,“Press me”按鈕的位置往上移動(dòng)了那么一點(diǎn)點(diǎn)

          11)在BIDBlueViewController和BIDYellowViewController中添加代碼
          在BIDBlueViewController.h中添加Action,如下

          @interface BIDBlueViewController : UIViewController - (IBAction)blueButtonPressed; @end

          在BIDBlueViewController.m中實(shí)現(xiàn)blueButtonPressed,如下

          復(fù)制代碼
          - (IBAction)blueButtonPressed {     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Blue View Button Pressed"                                                     message:@"You pressed the button on the blue view"                                                    delegate:nil                                           cancelButtonTitle:@"Yep, I did"                                           otherButtonTitles:nil];     [alert show]; }
          復(fù)制代碼

          在BIDYellowViewController.h添加Action,如下

          @interface BIDYellowViewController : UIViewController - (IBAction)yellowButtonPressed; @end

          在BIDYellowViewController.m中實(shí)現(xiàn)yellowButtonPressed,如下

          復(fù)制代碼
          - (IBAction)yellowButtonPressed {     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Yellow View Button Pressed"                                                     message:@"You pressed the button on the yellow view"                                                    delegate:nil                                           cancelButtonTitle:@"Yep, I did"                                           otherButtonTitles:nil];     [alert show]; }
          復(fù)制代碼

          代碼都很簡單,就不多做解釋了。

          12)關(guān)聯(lián)button和Action
          打開BlueView.xib,選中“Press me”按鈕,control-drag到File's owner釋放,在填出的框中選擇blueButtonPressed。
          打開YellowView.xib,選中“Press me”按鈕,control-drag到File's owner釋放,在填出的框中選擇yellowButtonPressed。

          13)編譯運(yùn)行
          至此我們已經(jīng)可以編譯運(yùn)行程序了,編譯成功后,iphone模擬器中顯示的效果(“Press me”按鈕的效果就不演示了)

          按下“Switch Views”按鈕,BlueSubview會(huì)切換到Y(jié)ellowSubview

          14)更炫的切換view的方法
          有沒有發(fā)現(xiàn)上面切換view效果很無聊,ios中有更炫的切換view的方法,使用動(dòng)畫的方式切換view,打開BIDSwitchViewController.m,重新編輯switchViews方法,如下

          復(fù)制代碼
          - (IBAction)switchViews:(id)sender {         [UIView beginAnimations:@"View Flip" context:nil];     [UIView setAnimationDuration:1.25];     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];          if(self.yellowViewController.view.superview == nil) {         if(self.yellowViewController == nil) {             self.yellowViewController = [[BIDYellowViewController alloc] initWithNibName:@"YellowView" bundle:nil];         }         [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];         [blueViewController.view removeFromSuperview];         [self.view insertSubview:self.yellowViewController.view atIndex:0];     } else {         if (self.blueViewController ==nil) {             self.blueViewController = [[BIDBlueViewController alloc] initWithNibName:@"BlueView" bundle:nil];         }         [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];         [yellowViewController.view removeFromSuperview];         [self.view insertSubview:self.blueViewController.view atIndex:0];     }     [UIView commitAnimations]; }
          復(fù)制代碼

          [UIViewbeginAnimations:@"View Flip" context:nil];
          先可以不用理解這句話的意思,因?yàn)槲乙矝]有理解,反正這行代碼是聲明一個(gè)animation block,前一個(gè)參數(shù)是設(shè)置animation block的title,后一個(gè)參數(shù)是設(shè)置一個(gè)對(duì)象,我也搞不清楚是干什么的,大概在以后的學(xué)習(xí)中會(huì)有所了解。

          [UIViewsetAnimationDuration:1.25];
          設(shè)定動(dòng)畫時(shí)間,即切換view的時(shí)間

          [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];
          設(shè)定動(dòng)畫的運(yùn)動(dòng)方式,開始慢,中間快,最后慢,大家開始看ios自己的說明吧。(An ease-in ease-out curve causes the animation to begin slowly, accelerate through the middle of its duration, and then slow again before completing. This is the default curve for most animations.)

          [UIViewsetAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
          設(shè)定切換的樣式,一共有4個(gè)值可以選:
          UIViewAnimationTransitionFlipFromRight
          UIViewAnimationTransitionFlipFromLeft
          UIViewAnimationTransitionFlipCurUp
          UIViewAnimationTransitionFlipCurDown
          這個(gè)大家自試試就可以知道了

          [UIViewcommitAnimations];
          當(dāng)所有動(dòng)畫的值設(shè)置完畢后,提交動(dòng)畫,之后view就會(huì)按照設(shè)定的動(dòng)畫進(jìn)行view的切換了。

          (此圖截于書本,因此不同清晰)

          15)總結(jié)
          ok,所有的功能都已經(jīng)完成了,在這個(gè)例子中,我們使用toolbar來完成對(duì)不同view的切換,我們需要一個(gè)root view進(jìn)行總的控制,然后多個(gè)subview進(jìn)行切換,最后還使用一個(gè)比較炫的效果進(jìn)行view之間的轉(zhuǎn)換,內(nèi)容很充實(shí)!

          View Switcher

          原文地址
          :http://www.cnblogs.com/minglz/archive/2012/12/11/2809368.html

          posted @ 2014-06-27 10:04 wokaoJune 閱讀(410) | 評(píng)論 (0)編輯 收藏

          <2014年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導(dǎo)航

          統(tǒng)計(jì)

          公告

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

          常用鏈接

          留言簿

          隨筆分類(26)

          隨筆檔案(29)

          文章分類

          最新隨筆

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 巴青县| 山丹县| 霞浦县| 鲁甸县| 茶陵县| 来安县| 香港 | 清镇市| 静海县| 博爱县| 宽城| 濉溪县| 乐业县| 定州市| 会昌县| 攀枝花市| 色达县| 桑植县| 诏安县| 奉节县| 会理县| 苍山县| 衡阳市| 洪江市| 五寨县| 屯门区| 桐梓县| 常熟市| 宜良县| 永兴县| 紫云| 松溪县| 乌什县| 宜兴市| 曲靖市| 清涧县| 根河市| 孝昌县| 鹤庆县| 察哈| 彩票|