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

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

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

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

    2014年6月27日

    IOS開發筆記 多個tableView的使用

    UITableView是app開發中常用到的控件,功能很強大,多用于數據的顯示。下面以一個簡單的實例來介紹tableview的基本用法。(適合新手,高手飄過)



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



    代碼在原由基礎上進行的修改:

    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; //加入了用于保存數組的數組 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,元素為數組  
    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來設計標題,功能上豐富,變化多。  
    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. //指定有多少個分區(Section),默認為1  
    100.   
    101. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
    102.   
    103.     return [titleArraycount];  
    104.   
    105. }  
    106.   
    107.    
    108.   
    109. //指定每個分區中有多少行,默認為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中的元素,并根據每個元素(數組)來判斷分區中的行數。  
    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中每個分區所對應的元素(數組),并通過其來取值。 (大家要有想像力, 復制代碼試試就明白了)  
    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. }  

    轉自:http://www.cnblogs.com/top5/archive/2012/05/17/2506604.html


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

    IOS開發筆記 多個tableView的使用

    UITableView是app開發中常用到的控件,功能很強大,多用于數據的顯示。下面以一個簡單的實例來介紹tableview的基本用法。(適合新手,高手飄過)



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



    代碼在原由基礎上進行的修改:

    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; //加入了用于保存數組的數組 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,元素為數組  
    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來設計標題,功能上豐富,變化多。  
    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. //指定有多少個分區(Section),默認為1  
    100.   
    101. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
    102.   
    103.     return [titleArraycount];  
    104.   
    105. }  
    106.   
    107.    
    108.   
    109. //指定每個分區中有多少行,默認為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中的元素,并根據每個元素(數組)來判斷分區中的行數。  
    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中每個分區所對應的元素(數組),并通過其來取值。 (大家要有想像力, 復制代碼試試就明白了)  
    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. }  

    轉自:http://www.cnblogs.com/top5/archive/2012/05/17/2506604.html


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

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

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

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

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

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

    1)創建一個工程,選擇Empty Application

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

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

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

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

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

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

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

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

    Device Family中選擇iphone,點擊Next

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

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

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

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

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

    其中,@class是告訴BIDAppDelegate,后面的BIDSwitchViewController是一個類,應該以類的方式處理該對象,后面在聲明property的時候,BIDAppDelegate就知道BIDSwitchViewController是一個類,不會不認該對象。

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

    復制代碼
    #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; }  ......
    復制代碼

    首先import BIDSwitchViewController,然后聲明synthesize,對應于頭文件中的property。

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

    self.switchViewController = [[BIDSwitchViewControlleralloc] initWithNibName:@"SwitchView" bundle:nil];
    通過xib(在舊的版本中,xib被稱為nib,因此這里出現的是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頂部的狀態欄了。

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

    [self.window addSubview:switchView];
    將switchView設置成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修改成如下

    復制代碼
    #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
    復制代碼

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

    6)關聯BIDSwitchViewController和SwitchView.xib
    在Project Navigator中選中SwitchView.xib,在xib的dock中選中File's Owner

    然后在Identity inspector中將Class改成BIDSwitchViewController

    這樣就將SwitchView.xib關聯到了BIDSwitchViewController,如果我們選擇Connections inspector,會看到我們剛才在BIDSwitchViewController.h中定義的Action:switchViews出現在Received Actions,我們之后就可以將這個Action關聯到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上,放在最下方

    默認的,已經有一個button在Toolbar上了,雙擊button改變文字,將文字改成“Switch Views”

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

    打開Connections inspector,我們可以看到關聯后的情況

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

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

    釋放鼠標后,在填出的框中選則view,這樣就關聯好了。

    關聯好后,查看Connections inspector,也可以看到關聯后的結果

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

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

    2個#import這個很好理解,因為BIDSwitchViewController是root controller,會控制另外2個controller,因此需要把另外2個controller引入進來,這樣才可以對他們進行操作。
    2個synthesize對著頭文件中的2個property。

    復制代碼
    // 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]; }
    復制代碼

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

    復制代碼
    - (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];     } }
    復制代碼

    實現switchViews Action,上面的代碼還是很好理解的,首先判斷當前哪個subview是沒有superview的,因為這2個subview不會同時顯示,當blueSubview顯示時,YellowSubview就會從root view中移除,因此不會具有superview,當得知那個subview沒有superview,就說明應該顯示這個subview。知道該顯示哪個subview后,再判斷這個subview是否還存在(是否需要重新載入初始化),然后將另一個subview從superview中移除,再將subview顯示出來。

    復制代碼
    - (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;     } }
    復制代碼

    當ios的內存不夠用時,didReceiveMemoryWarning會被系統自動調用,來釋放可利用的內存。在這里如果哪個subview沒有顯示,就釋放該subview,騰出內存。

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

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

    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改變了,就需要重新關聯一下File's owner的view,方法和之前的一樣,選中File‘s Owner,control-drag到下面的View上,在彈出的框中選擇view,關聯完畢,2個xib都需要進行這個操作。

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

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

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

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

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

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

    在BIDBlueViewController.m中實現blueButtonPressed,如下

    復制代碼
    - (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]; }
    復制代碼

    在BIDYellowViewController.h添加Action,如下

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

    在BIDYellowViewController.m中實現yellowButtonPressed,如下

    復制代碼
    - (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]; }
    復制代碼

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

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

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

    按下“Switch Views”按鈕,BlueSubview會切換到YellowSubview

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

    復制代碼
    - (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]; }
    復制代碼

    [UIViewbeginAnimations:@"View Flip" context:nil];
    先可以不用理解這句話的意思,因為我也沒有理解,反正這行代碼是聲明一個animation block,前一個參數是設置animation block的title,后一個參數是設置一個對象,我也搞不清楚是干什么的,大概在以后的學習中會有所了解。

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

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

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

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

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

    View Switcher

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

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

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

    導航

    統計

    公告

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

    常用鏈接

    留言簿

    隨筆分類(26)

    隨筆檔案(29)

    文章分類

    最新隨筆

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 成人性生交大片免费看无遮挡| 日本免费一区二区三区四区五六区| 最近最新的免费中文字幕| 久久久久亚洲精品日久生情 | 美女视频黄频a免费| 国产成人免费a在线视频色戒| 亚洲gay片在线gv网站| 四只虎免费永久观看| 一级做a爰黑人又硬又粗免费看51社区国产精品视 | 国产V亚洲V天堂无码久久久| 免费无码黄网站在线看| 亚洲精品无码Av人在线观看国产| 青青操免费在线视频| 精品亚洲aⅴ在线观看| 国产卡二卡三卡四卡免费网址| 亚洲色丰满少妇高潮18p| 日韩免费一级毛片| 一级女性全黄生活片免费看| 亚洲中文字幕无码永久在线| 久久爰www免费人成| 国产婷婷综合丁香亚洲欧洲| 亚洲夜夜欢A∨一区二区三区| 一级特黄录像免费播放中文版| 亚洲自偷自偷偷色无码中文| 久久久久成人片免费观看蜜芽| 亚洲国产av美女网站| 国产精品极品美女免费观看| 男女拍拍拍免费视频网站| 亚洲狠狠久久综合一区77777| 成人免费在线看片| 国产精品亚洲二区在线| 亚洲国产精品一区二区成人片国内| 91成人在线免费视频| 亚洲sm另类一区二区三区| 精品国产亚洲男女在线线电影| 日韩av无码久久精品免费| 亚洲熟妇无码八V在线播放| 亚洲福利精品一区二区三区| 99久久免费精品视频| 亚洲日韩一区精品射精| 国产亚洲精品观看91在线|