亚洲日本va午夜中文字幕一区,久久精品国产精品亚洲艾草网美妙,亚洲国产精品无码中文字http://www.tkk7.com/chenying19890808/category/54498.html<span style="color: red"><span style="font-family: 幼圓"><span style="color: red; font-family: ">自己選擇的路,摸爬滾打也要走下去</span> </span></span> zh-cnWed, 20 Aug 2014 02:55:44 GMTWed, 20 Aug 2014 02:55:44 GMT60IOS開發(fā)筆記 多個(gè)tableView的使用http://www.tkk7.com/chenying19890808/archive/2014/08/20/417150.htmlwokaoJunewokaoJuneWed, 20 Aug 2014 02:44:00 GMThttp://www.tkk7.com/chenying19890808/archive/2014/08/20/417150.htmlhttp://www.tkk7.com/chenying19890808/comments/417150.htmlhttp://www.tkk7.com/chenying19890808/archive/2014/08/20/417150.html#Feedback0http://www.tkk7.com/chenying19890808/comments/commentRss/417150.htmlhttp://www.tkk7.com/chenying19890808/services/trackbacks/417150.htmlUITableView是app開發(fā)中常用到的控件,功能很強(qiáng)大,多用于數(shù)據(jù)的顯示。下面以一個(gè)簡(jiǎn)單的實(shí)例來介紹tableview的基本用法。(適合新手,高手飄過)


  1. @interface TableViewTestViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{  
  2.   
  3.     UITableView *DataTable;  
  4.   
  5.     NSMutableArray *dataArray1; //定義數(shù)據(jù)數(shù)組1  
  6.   
  7.     NSMutableArray *dataArray2;//定義數(shù)據(jù)數(shù)組2  
  8.   
  9.     NSMutableArray *titleArray;//定義標(biāo)題數(shù)組  
  10.   
  11. }  
  12.   
  13.    
  14.   
  15. - (void)viewDidLoad  
  16.   
  17. {  
  18.   
  19.     [superviewDidLoad];  
  20.   
  21. //初始化tableview  
  22.   
  23.     DataTable = [[UITableViewalloc] initWithFrame:CGRectMake(0, 0, 320, 420)];//指定位置大小  
  24.   
  25.     [DataTablesetDelegate:self];//指定委托  
  26.   
  27.     [DataTablesetDataSource:self];//指定數(shù)據(jù)委托  
  28.   
  29.     [self.viewaddSubview:DataTable];//加載tableview  
  30.   
  31.       
  32.   
  33.     dataArray1 = [[NSMutableArrayalloc] initWithObjects:@"中國(guó)", @"美國(guó)", @"英國(guó)", nil];//初始化數(shù)據(jù)數(shù)組1  
  34.   
  35.     dataArray2 = [[NSMutableArrayalloc] initWithObjects:@"黃種人", @"黑種人", @"白種人", nil];//初始化數(shù)據(jù)數(shù)組2  
  36.   
  37.     titleArray = [[NSMutableArrayalloc] initWithObjects:@"國(guó)家", @"種族", 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:@"中國(guó)", @"美國(guó)", @"英國(guó)", nil];  
  34.   
  35.     dataArray2 = [[NSMutableArrayalloc] initWithObjects:@"黃種人", @"黑種人", @"白種人", nil];  
  36.   
  37.     titleArray = [[NSMutableArrayalloc] initWithObjects:@"國(guó)家", @"種族", 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




wokaoJune 2014-08-20 10:44 發(fā)表評(píng)論
]]>
IOS開發(fā)筆記 多個(gè)tableView的使用http://www.tkk7.com/chenying19890808/archive/2014/08/20/417151.htmlwokaoJunewokaoJuneWed, 20 Aug 2014 02:44:00 GMThttp://www.tkk7.com/chenying19890808/archive/2014/08/20/417151.htmlhttp://www.tkk7.com/chenying19890808/comments/417151.htmlhttp://www.tkk7.com/chenying19890808/archive/2014/08/20/417151.html#Feedback0http://www.tkk7.com/chenying19890808/comments/commentRss/417151.htmlhttp://www.tkk7.com/chenying19890808/services/trackbacks/417151.htmlUITableView是app開發(fā)中常用到的控件,功能很強(qiáng)大,多用于數(shù)據(jù)的顯示。下面以一個(gè)簡(jiǎn)單的實(shí)例來介紹tableview的基本用法。(適合新手,高手飄過)


  1. @interface TableViewTestViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{  
  2.   
  3.     UITableView *DataTable;  
  4.   
  5.     NSMutableArray *dataArray1; //定義數(shù)據(jù)數(shù)組1  
  6.   
  7.     NSMutableArray *dataArray2;//定義數(shù)據(jù)數(shù)組2  
  8.   
  9.     NSMutableArray *titleArray;//定義標(biāo)題數(shù)組  
  10.   
  11. }  
  12.   
  13.    
  14.   
  15. - (void)viewDidLoad  
  16.   
  17. {  
  18.   
  19.     [superviewDidLoad];  
  20.   
  21. //初始化tableview  
  22.   
  23.     DataTable = [[UITableViewalloc] initWithFrame:CGRectMake(0, 0, 320, 420)];//指定位置大小  
  24.   
  25.     [DataTablesetDelegate:self];//指定委托  
  26.   
  27.     [DataTablesetDataSource:self];//指定數(shù)據(jù)委托  
  28.   
  29.     [self.viewaddSubview:DataTable];//加載tableview  
  30.   
  31.       
  32.   
  33.     dataArray1 = [[NSMutableArrayalloc] initWithObjects:@"中國(guó)", @"美國(guó)", @"英國(guó)", nil];//初始化數(shù)據(jù)數(shù)組1  
  34.   
  35.     dataArray2 = [[NSMutableArrayalloc] initWithObjects:@"黃種人", @"黑種人", @"白種人", nil];//初始化數(shù)據(jù)數(shù)組2  
  36.   
  37.     titleArray = [[NSMutableArrayalloc] initWithObjects:@"國(guó)家", @"種族", 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:@"中國(guó)", @"美國(guó)", @"英國(guó)", nil];  
  34.   
  35.     dataArray2 = [[NSMutableArrayalloc] initWithObjects:@"黃種人", @"黑種人", @"白種人", nil];  
  36.   
  37.     titleArray = [[NSMutableArrayalloc] initWithObjects:@"國(guó)家", @"種族", 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




wokaoJune 2014-08-20 10:44 發(fā)表評(píng)論
]]>
主站蜘蛛池模板: 久久亚洲AV成人无码国产最大| 久久精品亚洲精品国产色婷| 久久精品熟女亚洲av麻豆| 免费乱码中文字幕网站| 成人A毛片免费观看网站| 亚洲人成影院在线| 好先生在线观看免费播放 | 一级特级女人18毛片免费视频| 最新精品亚洲成a人在线观看| 在线免费观看亚洲| 午夜亚洲国产精品福利| 久久亚洲国产视频| 国产精品久免费的黄网站| 182tv免费视频在线观看| 亚洲中文字幕乱码一区| 亚洲人成精品久久久久| 毛片免费在线播放| 国产精品高清免费网站| 2020久久精品亚洲热综合一本| 亚洲一区二区视频在线观看 | 久久精品国产精品亚洲艾草网| 最近中文字幕mv免费高清视频7 | 亚洲日本va中文字幕久久| 亚洲精品国产日韩无码AV永久免费网 | 老湿机一区午夜精品免费福利| 亚洲第一精品在线视频| 亚洲人成色777777在线观看| 亚洲精品无码专区久久久| 成人免费看片又大又黄| 成人奭片免费观看| 我要看免费的毛片| 日韩免费一级毛片| 114一级毛片免费| 成人毛片100免费观看| 中美日韩在线网免费毛片视频| 一区二区三区免费精品视频| 一级做a爱过程免费视频高清| 成人a毛片视频免费看| 伊人久久亚洲综合影院首页| 亚洲av无码不卡久久| 亚洲伦理一区二区|