//
// RootViewController.m
// UI10_UITableView2
//
// Created by Marry W. on 12/18/15.
// Copyright (c) 2015 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

import "RootViewController.h"

import "NSString+Characters.h"

@interface RootViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *arr;// 保存key
@property (nonatomic, retain) NSMutableDictionary *contactDic;// 保存所有联系人
@property (nonatomic, retain) UITableView *tableView;

@end

@implementation RootViewController

  • (void)dealloc
    {

    [_arr release];
    [super dealloc];

    }

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    // tableView
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];

    // 数据处理
    [self data];

    }

  • (void)data
    {
    // plist文件 属性列表
    // 获取文件路径
    NSString *path = [[NSBundle mainBundle]pathForResource:@"ContactList" ofType:@"plist"];
    // 创建数组
    NSArray *tempArr = [NSArray arrayWithContentsOfFile:path];

    // 临时数组保存数据源
    // NSArray *tempArr = @[@{@"name": @"恩叔", @"sex": @"男", @"phone": @"111"}, @{@"name": @"大娃", @"sex": @"男", @"phone": @"222"}, @{@"name": @"石美妮", @"sex": @"女", @"phone": @"333"}, @{@"name": @"三爷", @"sex": @"男", @"phone": @"444"} , @{@"name": @"张伟", @"sex": @"男", @"phone": @"555"}, @{@"name": @"恩叔2", @"sex": @"男", @"phone": @"666"}];
    // 初始化字典
    self.contactDic = [NSMutableDictionary dictionary];
    // 遍历数据源
    for (NSDictionary *dic in tempArr) {

    // 获取到每个字典中 name的首字母
    NSString *captialLetter = [dic[@"name"] firstCharacterOfName];
    // 先通过首字母 从联系人字典中获取对应的数组
    NSMutableArray *contactArr = _contactDic[captialLetter];
    // 判断 数组是否存在
    // 如果存在 直接添加
    if (contactArr) {
        [contactArr addObject:dic];
    } else {
        // 如果不存在 则新建数组
        NSMutableArray *newArr = [NSMutableArray array];
        // 添加联系人
        [newArr addObject:dic];
        // 添加到联系人字典中
        [_contactDic setObject:newArr forKey:captialLetter];
    }

    }
    // 获取有序的key数组
    self.arr = [[_contactDic.allKeys sortedArrayUsingSelector:@selector(compare:)] mutableCopy];
    }

// 分区数

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    return _contactDic.count;
    }

// 分区名

  • (NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section
    {
    return _arr[section];
    }

// 每个分区的行数

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    // 获取key
    NSString *key = _arr[section];
    // 获取key对应的联系人数组
    NSArray *arr = _contactDic[key];
    return arr.count;
    }
  • (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"] autorelease];

    }
    // 字面量
    // cell.textLabel.text = _arrindexPath.row;
    // 方法调用
    // cell.textLabel.text = [[_arr objectAtIndex:indexPath.row] objectForKey:@"name"];

// cell.detailTextLabel.text = [[_arr objectAtIndex:indexPath.row] objectForKey:@"phone"];


// 获取key
NSString *key = _arr[indexPath.section];
// 获取key对应的数组
NSArray *arr = _contactDic[key];
// 获取每一个联系人
NSDictionary *dic = arr[indexPath.row];
// 设置
cell.textLabel.text = dic[@"name"];
cell.detailTextLabel.text = dic[@"phone"];

return cell;

}
// 右侧索引

  • (NSArray )sectionIndexTitlesForTableView:(UITableView )tableView
    {
    return _arr;
    }
  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

/*

pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

  • (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.

    }

*/

@end