---
title: "新风作浪博客学习(十三)表视图的分组分区和索引分区 ."
author: "android_zhang"
date: "2013-06-07T08:50:08Z"
tags: ["iOS"]
categories: ["移动开发"]
slug: "untitled"
draft: false
lastmod: "2013-01-19T12:03:54Z"
id: 1772449
url: "https://android-zhang.iteye.com/blog/1772449"
---

本次实现的是表视图的分区和索引，代码和前面都差不多，主要还是代理方法的设计实现；



1.新建工程名为Partitation ， File->New->Project ->single View Application -> next

![](http://dl.iteye.com/upload/attachment/0079/5235/7f3a5e87-3076-3a25-92b0-b1ba3d404397.png)






2.添加协议和声明变量

```java
#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文件的添加

![](http://dl.iteye.com/upload/attachment/0079/5237/11a28ba8-7f2b-321b-8a72-88a9ad478dcf.png)



```java
- (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文件有几个分区

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

```java
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    从数组中读取分区的对应的key
    NSString *stringDataKey = [self.arraydata objectatindex:section];
//    根据读取的key从该分区字典中读取数据元素
    NSArray *arraySection = [self.dicdata objectforkey:stringdatakey];
//    返回的是特定分区的行数
    return [arraysection count];
}
```



根据索引路径获取分区行，需要使用哪个值在从字典中取出来

```java
-(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;
}
```



给每个分区指定可选标题值 

```java
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *key = [self.arraydata objectatindex:section];
    return key;
}
```


运行下效果

![](http://dl.iteye.com/upload/attachment/0079/5239/6754dad2-e028-3124-8d65-0c9071c25967.png)







5.添加索引，当我们查询的东西有几万条向下拖动表视图很显然比较麻烦，添加索引之后可以直接跳到所选的分区

```java
-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.arrayData;
}
```


运行效果

![](http://dl.iteye.com/upload/attachment/0079/5241/114bad4e-8e47-3e4e-87fd-08e468c27bcb.png)



![](http://dl.iteye.com/upload/attachment/0079/5243/ff75c8fa-211a-3675-a89d-1cd090f7f776.png)