詳解iPhone開發應用之表視圖分組實現代碼

來源:互聯網
上載者:User

iPhone開發應用中表視圖分組的實現是本文要介紹的內容,主要是以代碼實現視圖的分組,不多說,先來看本文詳細內容,貼一張圖:

1.先建立 plist檔案,

2.主介面 放置一個 table view控制項

3.介面代碼

 
  1. @interface SectionsViewController : UIViewController <UITableViewDataSource,UITableViewDelegate> {  
  2. NSDictionary *names;  
  3. NSArray *keys;  
  4. }  
  5. @property (nonatomic, retain) NSDictionary *names;  
  6. @property (nonatomic, retain) NSArray *keys;  
  7.  
  8. 4.實現代碼  
  9.  
  10. @implementation SectionsViewController  
  11. @synthesize names;  
  12. @synthesize keys;  
  13.  
  14. - (void)viewDidLoad {  
  15. NSString *path=[[NSBundle mainBundle] pathForResource:@"sortednames"   
  16.   ofType:@"plist"]; //擷取屬性列表的路徑,賦給path   
  17. NSDictionary *dict=[[NSDictionary alloc]   
  18. initWithContentsOfFile:path];  //將 路徑path下的資料表 初始化字典dict  
  19. self.names = dict; //字典dict 賦給names  
  20.  
  21. //因為 names 有 retain 屬性。 當給names賦值時 ,dict會自動retain增一),此時dict的retain count=2;  
  22. [dict release];  
  23. //for (int i=0; i<[[names allKeys] count]; i++) {  
  24.  
  25. // NSLog(@"%@\n",[[names allKeys] objectAtIndex:i]);  
  26. // }  
  27. //  
  28. NSArray *array=[[names allKeys] sortedArrayUsingSelector:  
  29. @selector(compare:)]; //給所有 keys 值按字母順序排序  
  30. //for (int i=0; i<[array count]; i++) {  
  31. // NSLog(@"%@\n",[array objectAtIndex:i]);  
  32. // }  
  33.  
  34. self.keys = array; //將 array對象賦給 keys  
  35.  
  36.    
  37. }  
  38.  
  39. - (void)didReceiveMemoryWarning {  
  40. // Releases the view if it doesn't have a superview.  
  41.     [super didReceiveMemoryWarning];  
  42. // Release any cached data, images, etc that aren't in use.  
  43. }  
  44. - (void)viewDidUnload {  
  45. // Release any retained subviews of the main view.  
  46. // e.g. self.myOutlet = nil;  
  47. self.names = nil;  
  48. self.keys = nil;  
  49. }  
  50.  
  51. - (void)dealloc {  
  52. [names release];  
  53. [keys release];  
  54.     [super dealloc];  
  55. }  
  56.  
  57. #pragma mark -  
  58. #pragma mark Table View Data Source Methods  
  59. // 返回有多少個分區  
  60. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  61. {  
  62.     return [keys count];  //擷取分區的數量  
  63. }  
  64. //返回 每個分區 有多少行  
  65. - (NSInteger)tableView:(UITableView *)tableView   
  66.  numberOfRowsInSection:(NSInteger)section  
  67. {  
  68.     NSString *key = [keys objectAtIndex:section]; //section為其中一個分區,擷取section的索引  
  69.     NSArray *nameSection = [names objectForKey:key];  //根據索引擷取分區裡面的所有資料  
  70.     return [nameSection count];     //返回分區裡的行的數量  
  71. }  
  72.  
  73. //返回 當前需要顯示的cell, 可能是 為了節省記憶體  
  74. - (UITableViewCell *)tableView:(UITableView *)tableView   
  75.          cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  76. {  
  77. //NSLog(@"tianshi\n");  
  78.     NSUInteger section = [indexPath section];//返回第幾分區  
  79.     NSUInteger row = [indexPath row];//擷取第幾分區的第幾行  
  80.     NSString *key = [keys objectAtIndex:section]; //返回 分區的索引key  
  81.     NSArray *nameSection = [names objectForKey:key];//返回 根據key獲得:當前分區的所有內容,  
  82.     static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";  
  83. //判斷cell是否存在,如果沒有,則建立一個  
  84.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:  
  85.                              SectionsTableIdentifier ];  
  86.     if (cell == nil) {  
  87.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault   
  88.                                        reuseIdentifier: SectionsTableIdentifier ] autorelease];  
  89.     }  
  90.     //給cell賦值  
  91.     cell.textLabel.text = [nameSection objectAtIndex:row];  
  92.     return cell;  
  93. }  
  94.  
  95. //為每一個分區指定一個名稱,現在的名稱為key的值  
  96. - (NSString *)tableView:(UITableView *)tableView   
  97. titleForHeaderInSection:(NSInteger)section  
  98. {  
  99.     NSString *key = [keys objectAtIndex:section];  
  100.     return key;  
  101. }  
  102. //添加索引的值,為右側的A----E  
  103. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView  
  104. {  
  105.     return keys;  

小結:詳解iPhone開發應用之表視圖分組實現代碼的內容介紹完了,希望本文對你有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.