详解iPhone开发应用之表视图分组实现代码

移动开发 iOS
本文介绍的是iPhone开发应用中表视图分组的实现,主要是以代码实现视图的分组内容,先来看本文详细讲解。

iPhone开发应用中表视图分组的实现是本文要介绍的内容,主要是以代码实现视图的分组,不多说,先来看本文详细内容,贴一张图:

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开发应用之表视图分组实现代码的内容介绍完了,希望本文对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-08-15 18:02:32

iPhone开发表视图

2011-08-12 10:04:24

iPhone开发视图

2011-08-11 11:51:07

iPhone键盘

2011-08-17 15:10:21

iPhone开发Web视图

2011-08-11 10:27:37

iPhoneUIView视图

2011-08-11 10:16:23

iPhoneUIView视图

2011-08-16 19:02:23

iPhone开发绘图

2011-08-10 10:23:20

iPhoneArchivingNSCoder

2011-07-25 14:44:41

iPhone iPhone开发 截屏

2011-08-15 10:15:00

iPhone开发警告框

2011-08-12 11:31:46

iPhoneUIView动画

2011-08-16 14:54:12

iphone开发APP

2011-08-15 11:23:41

iPhone开发循环滚动UIScrollVie

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2011-08-19 14:27:29

iPhone开发

2011-08-15 11:13:06

IOS开发并发Dispatch Qu

2011-07-20 15:20:14

IPhone AVAudioRec

2011-08-19 10:05:30

iPhone开发

2011-08-12 14:33:06

iPhone缓存文件

2011-07-27 11:14:37

iPhone UITableVie
点赞
收藏

51CTO技术栈公众号