分享

表视图(分区分组)

 叹落花 2014-12-20

对表视图进行分组与分区,便于用户对信息的查找。首先需要创建.plist的文件,包含所有的信息,便于表视图加载过程中数据的录入。用字典存储,以数组的方式获取表视图的数据。遵循UITableViewDelegate协议实现对表视图的分区。

首先添加、编写.plist文件,写入数据,也可导入:



在.h文件中添加协议,创建对象:

  1. @interface LinViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>  
  2. //创建表视图的对象  
  3. @property (retain, nonatomic) UITableView * mTableView;  
  4. //创建字典对象  
  5. @property (retain, nonatomic) NSDictionary * mDictionary;  
  6. //创建数组对象  
  7. @property (retain, nonatomic) NSArray * mArray;  
  8.   
  9. @end  

在.m文件里添加实现的方法:
  1. @implementation LinViewController  
  2.   
  3. //释放创建的对象  
  4. - (void)dealloc  
  5. {  
  6.     [_mTableView release];  
  7.     [_mDictionary release];  
  8.     [_mArray release];  
  9.     [super dealloc];  
  10. }  
  11. //加载视图  
  12. - (void)viewDidLoad  
  13. {  
  14.     [super viewDidLoad];  
  15.     //创建初始化表视图  
  16.     self.mTableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];  
  17.     //设置委托对象  
  18.     self.mTableView.dataSource = self;  
  19.     self.mTableView.delegate = self;  
  20.     //把表视图加载到当前视图中  
  21.     [self.view addSubview:self.mTableView];  
  22.       
  23.     //获取.plist文件的路径  
  24.     NSString * path = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"];  
  25.     //初始化字典  
  26.     self.mDictionary = [NSDictionary dictionaryWithContentsOfFile:path];  
  27.     //初始化数组,根据字典对象获取的数据  
  28.     self.mArray = [[self.mDictionary allKeys]sortedArrayUsingSelector:@selector(compare:)];  
  29.     //此处关联的方法是排列方式显示的方法,此时表示按字母顺序排列  
  30. }  
  31. #pragma mark---UITableViewDataSource---------  
  32. //根据数组元素获取表视图的行数  
  33. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  34. {  
  35.     return [[self.mDictionary objectForKey:[self.mArray objectAtIndex:section]]count];  
  36. }  
  37. //绘制每一行表  
  38. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  39. {  
  40.     //设置一个静态字符串做标签,(静态防止局部变量多次创建,提高效率,节约内存)  
  41.     static NSString * identifer = @"identifer";  
  42.     //创建一个cell对象(利用表示图可以复用的机制)  
  43.     UITableViewCell * pCell = [tableView dequeueReusableCellWithIdentifier:identifer];  
  44.     //如果cell为空,就创建一个新的出来  
  45.     if (nil == pCell)  
  46.     {  
  47.         pCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifer];  
  48.     }  
  49.     //获取所在的区域  
  50.     NSInteger section = [indexPath section];  
  51.     //获取当前表视图行数  
  52.     NSInteger row = [indexPath row];  
  53.     //把数组中对应元素传递给视图的文本  
  54.     pCell.textLabel.text =  [[self.mDictionary objectForKey:[self.mArray objectAtIndex:section]]objectAtIndex:row];  
  55.     return pCell;  
  56. }  
  57. //显示区域的表头标题  
  58. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section  
  59. {  
  60.     return [self.mArray objectAtIndex:section];  
  61. }  
  62. //显示区域的索引  
  63. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView  
  64. {  
  65.     return self.mArray;  
  66. }  
  67. #pragma mark---UITableViewDelegate-----------  
  68. //为表视图分区  
  69. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  70. {  
  71.     return [self.mArray count];  
  72. }  
  73.   
  74. - (void)didReceiveMemoryWarning  
  75. {  
  76.     [super didReceiveMemoryWarning];  
  77. }  
  78. end  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多