<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

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

    UITableView是app開發(fā)中常用到的控件,功能很強(qiáng)大,多用于數(shù)據(jù)的顯示。下面以一個簡單的實(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. //每個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. //指定有多少個分區(qū)(Section),默認(rèn)為1  
    86.   
    87. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
    88.   
    89.     return [titleArray count];//返回標(biāo)題數(shù)組中元素的個數(shù)來確定分區(qū)的個數(shù)  
    90.   
    91. }  
    92.   
    93.    
    94.   
    95. //指定每個分區(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];//每個分區(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ù)處理上具有很大的局限性。當(dāng)我們要從服務(wù)器上請求數(shù)據(jù),面對多種可能的數(shù)據(jù)(主要指數(shù)組的個數(shù)不穩(wěn)定)此時上面的switch將無法滿足我們的需求了。使用switch可是代碼的結(jié)構(gòu)清晰明了,但其局限性很致命(switch中case的個數(shù)無法動態(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.  //制定個性標(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. //每個section顯示的標(biāo)題,有了上面的這個就不要了  
    92.   
    93. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{  
    94.   
    95. }  
    96.   
    97.    
    98.   
    99. //指定有多少個分區(qū)(Section),默認(rèn)為1  
    100.   
    101. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
    102.   
    103.     return [titleArraycount];  
    104.   
    105. }  
    106.   
    107.    
    108.   
    109. //指定每個分區(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ù)每個元素(數(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 on 2014-08-20 10:44 wokaoJune 閱讀(2698) 評論(0)  編輯  收藏 所屬分類: IOS


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


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

    導(dǎo)航

    統(tǒng)計(jì)

    公告

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

    常用鏈接

    留言簿

    隨筆分類(26)

    隨筆檔案(29)

    文章分類

    最新隨筆

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 中文字幕免费高清视频| 亚洲国产一级在线观看| WWW亚洲色大成网络.COM| 亚洲国产天堂久久久久久| 亚洲视频免费在线观看| 亚洲另类自拍丝袜第五页| 亚洲夜夜欢A∨一区二区三区| 中文字幕亚洲免费无线观看日本 | 亚洲熟女一区二区三区| 91久久成人免费| 亚洲一级片免费看| 亚洲最大黄色网址| 国产猛烈高潮尖叫视频免费| 免费无码黄网站在线看| 亚洲国产一区二区三区在线观看| 亚洲区小说区图片区QVOD| 免费网站看v片在线香蕉| 国产精品免费看久久久| 菠萝菠萝蜜在线免费视频| 麻豆亚洲av熟女国产一区二| 亚洲国产精品毛片av不卡在线| 中文字幕免费视频| 国精产品一区一区三区免费视频| 亚洲精品国产精品| 亚洲精品视频在线观看视频| 亚洲1区1区3区4区产品乱码芒果| 亚洲伦乱亚洲h视频| 成人午夜视频免费| 亚洲一区免费视频| 在线看片免费人成视频福利| 特级毛片A级毛片100免费播放 | 免费毛片毛片网址| 亚洲偷自精品三十六区| 亚洲卡一卡2卡三卡4卡无卡三| 国产L精品国产亚洲区久久| 国产特级淫片免费看| 久久不见久久见中文字幕免费 | 日本在线免费播放| 国产一级黄片儿免费看| 免费国产a理论片| 精品国产亚洲第一区二区三区|