表视图的分组分区和索引分区

本文介绍了如何在iOS应用中实现表视图的分区和索引功能,通过加载plist文件数据,设置代理方法来动态生成分区和行数。详细讲解了`numberOfSectionsInTableView`、`tableView:numberOfRowsInSection:`、`tableView:cellForRowAtIndexPath:`和`titleForHeaderInSection:`的方法实现,以及如何添加索引以方便快速浏览大量数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        本次实现的是表视图的分区和索引,代码和前面都差不多,主要还是代理方法的设计实现;

1.新建工程名为Partitation , File->New->Project ->single View Application -> next


2.添加协议和声明变量

[cpp]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface PartitionViewController : UIViewController  
  4.           <UITableViewDelegate,UITableViewDataSource>  
  5. @property (strong,nonatomic) UITableView *partitationTableView;  
  6. @property (strong,nonatomic) UITableViewCell *partitionTableViewCell;  
  7. @property (strong,nonatomic) NSDictionary *dicData;  
  8. @property (strong,nonatomic) NSArray *arrayData;  
  9.   
  10. @end  


3.添加plist文件,再到ViewDidLoad中初始化视图

plist文件的添加


[cpp]  view plain copy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.       
  5.     UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];  
  6.       
  7.     [self.view addSubview:navBar];  
  8.   
  9.     // Do any additional setup after loading the view, typically from a nib.  
  10.       
  11.     self.partitationTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];  
  12.       
  13.     self.partitationTableView.delegate=self;  
  14.     self.partitationTableView.dataSource = self;  
  15.     [self.view addSubview:self.partitationTableView];  
  16.       
  17.     [self.view addSubview:self.partitationTableView];  
  18.       
  19. //    读取plist文件  
  20.     NSBundle *bundle = [NSBundle mainBundle];  
  21.     NSURL *plistURL = [bundle URLForResource:@"partitionInfo" withExtension:@"plist"];  
  22.       
  23.     NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfURL:plistURL];  
  24.     self.dicData = dictionary;  
  25.       
  26. //    读取字典中的key储存数组中  
  27.     NSArray *array = [self.dicData allKeys];  
  28.     self.arrayData = array;  
  29.       
  30. }  

4.实现委托方法

前面我们做的都是return 1,表示返回的是一个分区,现在是从数组中读取字典中的plist文件有几个分区

[cpp]  view plain copy
  1. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  2. {  
  3.     return [self.arrayData count];  
  4. }  


[cpp]  view plain copy
  1. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  2. {  
  3. //    从数组中读取分区的对应的key  
  4.     NSString *stringDataKey = [self.arrayData objectAtIndex:section];  
  5. //    根据读取的key从该分区字典中读取数据元素  
  6.     NSArray *arraySection = [self.dicData objectForKey:stringDataKey];  
  7. //    返回的是特定分区的行数  
  8.     return [arraySection count];  
  9. }  
根据索引路径获取分区行,需要使用哪个值在从字典中取出来
[cpp]  view plain copy
  1. -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     NSInteger section = [indexPath section];  
  4.     NSInteger row = [indexPath row];  
  5.       
  6.     NSString *key = [self.arrayData objectAtIndex:section];  
  7.     NSArray *arraySection = [self.dicData objectForKey:key];  
  8.       
  9.     static NSString *partitation = @"partitation";  
  10.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:partitation];  
  11.       
  12.     if (cell==nil) {  
  13.         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:partitation];  
  14.     }  
  15.       
  16.     cell.textLabel.text = [arraySection objectAtIndex:row];  
  17.     return cell;  
  18. }  


给每个分区指定可选标题值

[cpp]  view plain copy
  1. -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section  
  2. {  
  3.     NSString *key = [self.arrayData objectAtIndex:section];  
  4.     return key;  
  5. }  

运行下效果

  


5.添加索引,当我们查询的东西有几万条向下拖动表视图很显然比较麻烦,添加索引之后可以直接跳到所选的分区

[cpp]  view plain copy
  1. -(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView  
  2. {  
  3.     return self.arrayData;  
  4. }  
运行效果

  




附上源代码:http://download.youkuaiyun.com/detail/duxinfeng2010/4419302

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值