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

          2014年6月16日

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

          UITableView是app開發(fā)中常用到的控件,功能很強大,多用于數(shù)據(jù)的顯示。下面以一個簡單的實例來介紹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;//定義標題數(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];//初始化標題數(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. //每個section顯示的標題  
          60.   
          61. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{  
          62.   
          63.     switch (section) {  
          64.   
          65.         case 0:  
          66.   
          67.             return [titleArray objectAtIndex:section];//提取標題數(shù)組的元素用來顯示標題  
          68.   
          69.         case 1:  
          70.   
          71.             return [titleArray objectAtIndex:section];//提取標題數(shù)組的元素用來顯示標題  
          72.   
          73.         default:  
          74.   
          75.             return @"Unknown";  
          76.   
          77.     }  
          78.   
          79.    
          80.   
          81. }  
          82.   
          83.    
          84.   
          85. //指定有多少個分區(qū)(Section),默認為1  
          86.   
          87. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
          88.   
          89.     return [titleArray count];//返回標題數(shù)組中元素的個數(shù)來確定分區(qū)的個數(shù)  
          90.   
          91. }  
          92.   
          93.    
          94.   
          95. //指定每個分區(qū)中有多少行,默認為1  
          96.   
          97. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
          98.   
          99.     switch (section) {  
          100.   
          101.         case 0:  
          102.   
          103.            return  [dataArray1 count];//每個分區(qū)通常對應(yīng)不同的數(shù)組,返回其元素個數(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://對應(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ù)處理上具有很大的局限性。當我們要從服務(wù)器上請求數(shù)據(jù),面對多種可能的數(shù)據(jù)(主要指數(shù)組的個數(shù)不穩(wěn)定)此時上面的switch將無法滿足我們的需求了。使用switch可是代碼的結(jié)構(gòu)清晰明了,但其局限性很致命(switch中case的個數(shù)無法動態(tài)指定),下面的另一種方法可解決上述問題。



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

          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.  //制定個性標題,這里通過UIview來設(shè)計標題,功能上豐富,變化多。  
          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. //指定有多少個分區(qū)(Section),默認為1  
          100.   
          101. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
          102.   
          103.     return [titleArraycount];  
          104.   
          105. }  
          106.   
          107.    
          108.   
          109. //指定每個分區(qū)中有多少行,默認為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ù)每個元素(數(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中每個分區(qū)所對應(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 閱讀(311) | 評論 (0)編輯 收藏

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

          UITableView是app開發(fā)中常用到的控件,功能很強大,多用于數(shù)據(jù)的顯示。下面以一個簡單的實例來介紹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;//定義標題數(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];//初始化標題數(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. //每個section顯示的標題  
          60.   
          61. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{  
          62.   
          63.     switch (section) {  
          64.   
          65.         case 0:  
          66.   
          67.             return [titleArray objectAtIndex:section];//提取標題數(shù)組的元素用來顯示標題  
          68.   
          69.         case 1:  
          70.   
          71.             return [titleArray objectAtIndex:section];//提取標題數(shù)組的元素用來顯示標題  
          72.   
          73.         default:  
          74.   
          75.             return @"Unknown";  
          76.   
          77.     }  
          78.   
          79.    
          80.   
          81. }  
          82.   
          83.    
          84.   
          85. //指定有多少個分區(qū)(Section),默認為1  
          86.   
          87. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
          88.   
          89.     return [titleArray count];//返回標題數(shù)組中元素的個數(shù)來確定分區(qū)的個數(shù)  
          90.   
          91. }  
          92.   
          93.    
          94.   
          95. //指定每個分區(qū)中有多少行,默認為1  
          96.   
          97. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
          98.   
          99.     switch (section) {  
          100.   
          101.         case 0:  
          102.   
          103.            return  [dataArray1 count];//每個分區(qū)通常對應(yīng)不同的數(shù)組,返回其元素個數(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://對應(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ù)處理上具有很大的局限性。當我們要從服務(wù)器上請求數(shù)據(jù),面對多種可能的數(shù)據(jù)(主要指數(shù)組的個數(shù)不穩(wěn)定)此時上面的switch將無法滿足我們的需求了。使用switch可是代碼的結(jié)構(gòu)清晰明了,但其局限性很致命(switch中case的個數(shù)無法動態(tài)指定),下面的另一種方法可解決上述問題。



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

          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.  //制定個性標題,這里通過UIview來設(shè)計標題,功能上豐富,變化多。  
          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. //指定有多少個分區(qū)(Section),默認為1  
          100.   
          101. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
          102.   
          103.     return [titleArraycount];  
          104.   
          105. }  
          106.   
          107.    
          108.   
          109. //指定每個分區(qū)中有多少行,默認為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ù)每個元素(數(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中每個分區(qū)所對應(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 閱讀(2705) | 評論 (0)編輯 收藏

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

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

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

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

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

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

          這次不再選擇Single View Application,而選擇Empty Application,項目中的所有文件我們都會手動進行添加。

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

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

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

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

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

          BIDSwitchViewController是項目的最頂層的view controller(root controller),它用于控制另外2個view的切換,下面按照同樣的方法,創(chuàng)建另外2個view controller,一個名字教BIDBlueViewController,另一個叫做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,點擊Next按鈕

          Device Family中選擇iphone,點擊Next

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

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

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

          4)編輯BIDAppDelegate文件
          當一個app啟動的時候,我們都會默認的把一個view載入當前的iphone窗口(application's main window),在這個例子中,這個view就是我們的root view,即BIDSwitchViewController。我們是在BIDAppDelegate文件中設(shè)置默認載入的view的,因此首先打開BIDAppDelegate.h,添加class BIDSwitchViewController,和它的一個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是一個類,應(yīng)該以類的方式處理該對象,后面在聲明property的時候,BIDAppDelegate就知道BIDSwitchViewController是一個類,不會不認該對象。

          接著打開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,對應(yīng)于頭文件中的property。

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

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

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

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

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

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

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

          5)編輯BIDSwitchViewController.h
          BIDSwitchViewController是root controller,用于控制其他2個view的切換,因此需要在其頭文件中聲明其他兩個controller,然后需要定義一個Action,用來完成對2個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,會看到我們剛才在BIDSwitchViewController.h中定義的Action:switchViews出現(xiàn)在Received Actions,我們之后就可以將這個Action關(guān)聯(lián)到SwitchView.xib的控件上。

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

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

          拖動到View上,放在最下方

          默認的,已經(jīng)有一個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)后的情況

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

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

          釋放鼠標后,在填出的框中選則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個#import這個很好理解,因為BIDSwitchViewController是root controller,會控制另外2個controller,因此需要把另外2個controller引入進來,這樣才可以對他們進行操作。
          2個synthesize對著頭文件中的2個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,在這里對其重載,這個方法發(fā)生在當view已經(jīng)載入完成后,我們所要做的是當root view載入完成后,在載入root view的subview,在我們的這個例子中是BlueView。BlueView的載入方法和前面的一樣,使用initWithNibName,然后作為subView插入到當前的view中(當前的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ù)制代碼

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

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

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

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

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

          修改BlueView.xib的class為BIDBlueViewController,修改YellowView.xib的class為BIDYellowController(修改方法:選中xib,點擊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個xib都需要進行這個操作。

          在Attributes inspector中,將BlueView.xib的background顏色改成藍色

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

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

          仔細看的話,“Press me”按鈕的位置往上移動了那么一點點

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

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

          在BIDBlueViewController.m中實現(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中實現(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)編譯運行
          至此我們已經(jīng)可以編譯運行程序了,編譯成功后,iphone模擬器中顯示的效果(“Press me”按鈕的效果就不演示了)

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

          14)更炫的切換view的方法
          有沒有發(fā)現(xiàn)上面切換view效果很無聊,ios中有更炫的切換view的方法,使用動畫的方式切換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];
          先可以不用理解這句話的意思,因為我也沒有理解,反正這行代碼是聲明一個animation block,前一個參數(shù)是設(shè)置animation block的title,后一個參數(shù)是設(shè)置一個對象,我也搞不清楚是干什么的,大概在以后的學習中會有所了解。

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

          [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];
          設(shè)定動畫的運動方式,開始慢,中間快,最后慢,大家開始看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個值可以選:
          UIViewAnimationTransitionFlipFromRight
          UIViewAnimationTransitionFlipFromLeft
          UIViewAnimationTransitionFlipCurUp
          UIViewAnimationTransitionFlipCurDown
          這個大家自試試就可以知道了

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

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

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

          View Switcher

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

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

          常見Oracle HINT的用法

          在SQL語句優(yōu)化過程中,我們經(jīng)常會用到hint,現(xiàn)總結(jié)一下在SQL優(yōu)化過程中常見Oracle HINT的用法:
          1. /*+ALL_ROWS*/
          表明對語句塊選擇基于開銷的優(yōu)化方法,并獲得最佳吞吐量,使資源消耗最小化.
          例如:
          SELECT /*+ALL+_ROWS*/ EMP_NO,EMP_NAM,DAT_IN FROM BSEMPMS WHERE EMP_NO='SCOTT';
          2. /*+FIRST_ROWS*/
          表明對語句塊選擇基于開銷的優(yōu)化方法,并獲得最佳響應(yīng)時間,使資源消耗最小化.
          例如:
          SELECT /*+FIRST_ROWS*/ EMP_NO,EMP_NAM,DAT_IN FROM BSEMPMS WHERE EMP_NO='SCOTT';
          3. /*+CHOOSE*/
          表明如果數(shù)據(jù)字典中有訪問表的統(tǒng)計信息,將基于開銷的優(yōu)化方法,并獲得最佳的吞吐量;
          表明如果數(shù)據(jù)字典中沒有訪問表的統(tǒng)計信息,將基于規(guī)則開銷的優(yōu)化方法;
          例如:
          SELECT /*+CHOOSE*/ EMP_NO,EMP_NAM,DAT_IN FROM BSEMPMS WHERE EMP_NO='SCOTT';
          4. /*+RULE*/
          表明對語句塊選擇基于規(guī)則的優(yōu)化方法.
          例如:
          SELECT /*+ RULE */ EMP_NO,EMP_NAM,DAT_IN FROM BSEMPMS WHERE EMP_NO='SCOTT';
          5. /*+FULL(TABLE)*/
          表明對表選擇全局掃描的方法.
          例如:
          SELECT /*+FULL(A)*/ EMP_NO,EMP_NAM FROM BSEMPMS A WHERE EMP_NO='SCOTT';
          6. /*+ROWID(TABLE)*/
          提示明確表明對指定表根據(jù)ROWID進行訪問.
          例如:
          SELECT /*+ROWID(BSEMPMS)*/ * FROM BSEMPMS WHERE ROWID>='AAAAAAAAAAAAAA'
          AND EMP_NO='SCOTT';
          7. /*+CLUSTER(TABLE)*/
          提示明確表明對指定表選擇簇掃描的訪問方法,它只對簇對象有效.
          例如:
          SELECT /*+CLUSTER */ BSEMPMS.EMP_NO,DPT_NO FROM BSEMPMS,BSDPTMS
          WHERE DPT_NO='TEC304' AND BSEMPMS.DPT_NO=BSDPTMS.DPT_NO;
          8. /*+INDEX(TABLE INDEX_NAME)*/
          表明對表選擇索引的掃描方法.
          例如:
          SELECT /*+INDEX(BSEMPMS SEX_INDEX) USE SEX_INDEX BECAUSE THERE ARE FEWMALE BSEMPMS */ FROM BSEMPMS WHERE SEX='M';
          9. /*+INDEX_ASC(TABLE INDEX_NAME)*/
          表明對表選擇索引升序的掃描方法.
          例如:
          SELECT /*+INDEX_ASC(BSEMPMS PK_BSEMPMS) */ FROM BSEMPMS WHERE DPT_NO='SCOTT';
          10. /*+INDEX_COMBINE*/
          為指定表選擇位圖訪問路經(jīng),如果INDEX_COMBINE中沒有提供作為參數(shù)的索引,將選擇出位圖索引的布爾組合方式.
          例如:
          SELECT /*+INDEX_COMBINE(BSEMPMS SAL_BMI HIREDATE_BMI)*/ * FROM BSEMPMS
          WHERE SAL<5000000 AND HIREDATE<SYSDATE;
          11. /*+INDEX_JOIN(TABLE INDEX_NAME)*/
          提示明確命令優(yōu)化器使用索引作為訪問路徑.
          例如:
          SELECT /*+INDEX_JOIN(BSEMPMS SAL_HMI HIREDATE_BMI)*/ SAL,HIREDATE
          FROM BSEMPMS WHERE SAL<60000;
          12. /*+INDEX_DESC(TABLE INDEX_NAME)*/
          表明對表選擇索引降序的掃描方法.
          例如:
          SELECT /*+INDEX_DESC(BSEMPMS PK_BSEMPMS) */ FROM BSEMPMS WHERE DPT_NO='SCOTT';
          13. /*+INDEX_FFS(TABLE INDEX_NAME)*/
          對指定的表執(zhí)行快速全索引掃描,而不是全表掃描的辦法.
          例如:
          SELECT /*+INDEX_FFS(BSEMPMS IN_EMPNAM)*/ * FROM BSEMPMS WHERE DPT_NO='TEC305';
          14. /*+ADD_EQUAL TABLE INDEX_NAM1,INDEX_NAM2,...*/
          提示明確進行執(zhí)行規(guī)劃的選擇,將幾個單列索引的掃描合起來.
          例如:
          SELECT /*+INDEX_FFS(BSEMPMS IN_DPTNO,IN_EMPNO,IN_SEX)*/ * FROM BSEMPMS WHERE EMP_NO='SCOTT' AND DPT_NO='TDC306';
          15. /*+USE_CONCAT*/
          對查詢中的WHERE后面的OR條件進行轉(zhuǎn)換為UNION ALL的組合查詢.
          例如:
          SELECT /*+USE_CONCAT*/ * FROM BSEMPMS WHERE DPT_NO='TDC506' AND SEX='M';
          16. /*+NO_EXPAND*/
          對于WHERE后面的OR 或者IN-LIST的查詢語句,NO_EXPAND將阻止其基于優(yōu)化器對其進行擴展.
          例如:
          SELECT /*+NO_EXPAND*/ * FROM BSEMPMS WHERE DPT_NO='TDC506' AND SEX='M';
          17. /*+NOWRITE*/
          禁止對查詢塊的查詢重寫操作.
          18. /*+REWRITE*/
          可以將視圖作為參數(shù).
          19. /*+MERGE(TABLE)*/
          能夠?qū)σ晥D的各個查詢進行相應(yīng)的合并.
          例如:
          SELECT /*+MERGE(V) */ A.EMP_NO,A.EMP_NAM,B.DPT_NO FROM BSEMPMS A (SELET DPT_NO
          ,AVG(SAL) AS AVG_SAL FROM BSEMPMS B GROUP BY DPT_NO) V WHERE A.DPT_NO=V.DPT_NO
          AND A.SAL>V.AVG_SAL;
          20. /*+NO_MERGE(TABLE)*/
          對于有可合并的視圖不再合并.
          例如:
          SELECT /*+NO_MERGE(V) */ A.EMP_NO,A.EMP_NAM,B.DPT_NO FROM BSEMPMS A (SELECT DPT_NO,AVG(SAL) AS AVG_SAL FROM BSEMPMS B GROUP BY DPT_NO) V WHERE A.DPT_NO=V.DPT_NO AND A.SAL>V.AVG_SAL;
          21. /*+ORDERED*/
          根據(jù)表出現(xiàn)在FROM中的順序,ORDERED使ORACLE依此順序?qū)ζ溥B接.
          例如:
          SELECT /*+ORDERED*/ A.COL1,B.COL2,C.COL3 FROM TABLE1 A,TABLE2 B,TABLE3 C WHERE A.COL1=B.COL1 AND B.COL1=C.COL1;
          22. /*+USE_NL(TABLE)*/
          將指定表與嵌套的連接的行源進行連接,并把指定表作為內(nèi)部表.
          例如:
          SELECT /*+ORDERED USE_NL(BSEMPMS)*/ BSDPTMS.DPT_NO,BSEMPMS.EMP_NO,BSEMPMS.EMP_NAM FROM BSEMPMS,BSDPTMS WHERE BSEMPMS.DPT_NO=BSDPTMS.DPT_NO;
          23. /*+USE_MERGE(TABLE)*/
          將指定的表與其他行源通過合并排序連接方式連接起來.
          例如:
          SELECT /*+USE_MERGE(BSEMPMS,BSDPTMS)*/ * FROM BSEMPMS,BSDPTMS WHERE BSEMPMS.DPT_NO=BSDPTMS.DPT_NO;
          24. /*+USE_HASH(TABLE)*/
          將指定的表與其他行源通過哈希連接方式連接起來.
          例如:
          SELECT /*+USE_HASH(BSEMPMS,BSDPTMS)*/ * FROM BSEMPMS,BSDPTMS WHERE BSEMPMS.DPT_NO=BSDPTMS.DPT_NO;
          25. /*+DRIVING_SITE(TABLE)*/
          強制與ORACLE所選擇的位置不同的表進行查詢執(zhí)行.
          例如:
          SELECT /*+DRIVING_SITE(DEPT)*/ * FROM BSEMPMS,DEPT@BSDPTMS WHERE BSEMPMS.DPT_NO=DEPT.DPT_NO;
          26. /*+LEADING(TABLE)*/
          將指定的表作為連接次序中的首表.
          27. /*+CACHE(TABLE)*/
          當進行全表掃描時,CACHE提示能夠?qū)⒈淼臋z索塊放置在緩沖區(qū)緩存中最近最少列表LRU的最近使用端
          例如:
          SELECT /*+FULL(BSEMPMS) CAHE(BSEMPMS) */ EMP_NAM FROM BSEMPMS;
          28. /*+NOCACHE(TABLE)*/
          當進行全表掃描時,CACHE提示能夠?qū)⒈淼臋z索塊放置在緩沖區(qū)緩存中最近最少列表LRU的最近使用端
          例如:
          SELECT /*+FULL(BSEMPMS) NOCAHE(BSEMPMS) */ EMP_NAM FROM BSEMPMS;
          29. /*+APPEND*/
          直接插入到表的最后,可以提高速度.
          insert /*+append*/ into test1 select * from test4 ;
          30. /*+NOAPPEND*/
          通過在插入語句生存期內(nèi)停止并行模式來啟動常規(guī)插入.
          insert /*+noappend*/ into test1 select * from test4 ;





          參考blog:http://blog.csdn.net/tianlesoftware/article/details/4969702

          posted @ 2014-06-16 17:31 wokaoJune 閱讀(249) | 評論 (0)編輯 收藏

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

          導(dǎo)航

          統(tǒng)計

          公告

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

          常用鏈接

          留言簿

          隨筆分類(26)

          隨筆檔案(29)

          文章分類

          最新隨筆

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 蓝山县| 玉林市| 台安县| 中宁县| 南丰县| 库车县| 罗山县| 河间市| 马龙县| 南郑县| 广西| 肃宁县| 葫芦岛市| 田林县| 改则县| 庆城县| 天祝| 连山| 绥江县| 进贤县| 高唐县| 石楼县| 报价| 开平市| 陵川县| 靖州| 仁寿县| 双城市| 巧家县| 迁西县| 曲靖市| 安泽县| 济源市| 台中市| 枣强县| 获嘉县| 万盛区| 凤翔县| 英吉沙县| 浦东新区| 汝南县|