自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解iPhone開發(fā)應用之表視圖分組實現(xiàn)代碼

移動開發(fā) iOS
本文介紹的是iPhone開發(fā)應用中表視圖分組的實現(xiàn),主要是以代碼實現(xiàn)視圖的分組內(nèi)容,先來看本文詳細講解。

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

iPhone開發(fā)應用之表視圖分組實現(xiàn)代碼

1.先創(chuàng)建 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.實現(xiàn)代碼  
  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下的數(shù)據(jù)表 初始化字典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. // 返回有多少個分區(qū)  
  60. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  61. {  
  62.     return [keys count];  //獲取分區(qū)的數(shù)量  
  63. }  
  64. //返回 每個分區(qū) 有多少行  
  65. - (NSInteger)tableView:(UITableView *)tableView   
  66.  numberOfRowsInSection:(NSInteger)section  
  67. {  
  68.     NSString *key = [keys objectAtIndex:section]; //section為其中一個分區(qū),獲取section的索引  
  69.     NSArray *nameSection = [names objectForKey:key];  //根據(jù)索引獲取分區(qū)里面的所有數(shù)據(jù)  
  70.     return [nameSection count];     //返回分區(qū)里的行的數(shù)量  
  71. }  
  72.  
  73. //返回 當前需要顯示的cell, 可能是 為了節(jié)省內(nèi)存  
  74. - (UITableViewCell *)tableView:(UITableView *)tableView   
  75.          cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  76. {  
  77. //NSLog(@"tianshi\n");  
  78.     NSUInteger section = [indexPath section];//返回第幾分區(qū)  
  79.     NSUInteger row = [indexPath row];//獲取第幾分區(qū)的第幾行  
  80.     NSString *key = [keys objectAtIndex:section]; //返回 分區(qū)的索引key  
  81.     NSArray *nameSection = [names objectForKey:key];//返回 根據(jù)key獲得:當前分區(qū)的所有內(nèi)容,  
  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. //為每一個分區(qū)指定一個名稱,現(xiàn)在的名稱為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開發(fā)應用之表視圖分組實現(xiàn)代碼的內(nèi)容介紹完了,希望本文對你有所幫助!

責任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關推薦

2011-08-15 18:02:32

iPhone開發(fā)表視圖

2011-08-12 10:04:24

iPhone開發(fā)視圖

2011-08-11 11:51:07

iPhone鍵盤

2011-08-17 15:10:21

iPhone開發(fā)Web視圖

2011-08-11 10:16:23

iPhoneUIView視圖

2011-08-11 10:27:37

iPhoneUIView視圖

2011-08-16 19:02:23

iPhone開發(fā)繪圖

2011-08-10 10:23:20

iPhoneArchivingNSCoder

2011-07-25 14:44:41

iPhone iPhone開發(fā) 截屏

2011-08-12 11:31:46

iPhoneUIView動畫

2011-08-15 10:15:00

iPhone開發(fā)警告框

2011-08-16 14:54:12

iphone開發(fā)APP

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2011-08-15 11:13:06

IOS開發(fā)并發(fā)Dispatch Qu

2011-08-15 11:23:41

iPhone開發(fā)循環(huán)滾動UIScrollVie

2011-07-20 15:20:14

IPhone AVAudioRec

2011-08-19 14:27:29

iPhone開發(fā)

2011-08-19 10:05:30

iPhone開發(fā)

2011-08-12 14:33:06

iPhone緩存文件

2011-07-27 11:14:37

iPhone UITableVie
點贊
收藏

51CTO技術棧公眾號