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

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

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


2.添加协议和声明变量

#import <UIKit/UIKit.h>

@interface PartitionViewController : UIViewController
          <UITableViewDelegate,UITableViewDataSource>
@property (strong,nonatomic) UITableView *partitationTableView;
@property (strong,nonatomic) UITableViewCell *partitionTableViewCell;
@property (strong,nonatomic) NSDictionary *dicData;
@property (strong,nonatomic) NSArray *arrayData;

@end


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

plist文件的添加


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    
    [self.view addSubview:navBar];

	// Do any additional setup after loading the view, typically from a nib.
    
    self.partitationTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    
    self.partitationTableView.delegate=self;
    self.partitationTableView.dataSource = self;
    [self.view addSubview:self.partitationTableView];
    
    [self.view addSubview:self.partitationTableView];
    
//    读取plist文件
    NSBundle *bundle = [NSBundle mainBundle];
    NSURL *plistURL = [bundle URLForResource:@"partitionInfo" withExtension:@"plist"];
    
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfURL:plistURL];
    self.dicData = dictionary;
    
//    读取字典中的key储存数组中
    NSArray *array = [self.dicData allKeys];
    self.arrayData = array;
    
}

4.实现委托方法

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

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.arrayData count];
}


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


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

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *key = [self.arrayData objectAtIndex:section];
    return key;
}

运行下效果

  


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

-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.arrayData;
}
运行效果

  




附上源代码:http://download.csdn.net/detail/duxinfeng2010/4419302




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值