//
// DataHandle.m
// UI11_Contact
//
// Created by dllo on 15/11/23.
// Copyright (c) 2015年 dllo. All rights reserved.
//
#import "DataHandle.h"
#import "Hero.h"
@implementation DataHandle
//保证不同位置不同文件调用都访问同一块空间
+ (instancetype)shareData
{
//static 编译
//alloc 运行
static DataHandle *dataH = nil;
if (nil == dataH) {
dataH = [[DataHandle alloc] init];
[dataH getData];
}
return dataH;
}
- (void)getData
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Hero" ofType:@"plist"];
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
self.heroDic = [NSMutableDictionary dictionary];
for (NSString *key in dic) {
NSMutableArray *arr = [NSMutableArray array];
for (NSDictionary *tempDic in [dic objectForKey:key]) {
Hero *hero = [[Hero alloc] init];
[hero setValuesForKeysWithDictionary:tempDic];
[arr addObject:hero];
[hero release];
}
[self.heroDic setObject:arr forKey:key];
}
self.keyArr = [NSMutableArray arrayWithArray:self.heroDic.allKeys];
// [self.keyArr sortUsingComparator:^NSComparisonResult(NSString *str1, NSString *str2) {
// return [str1 compare:str2];
// }];
#warning keyArr存的是字符串 可以直接用compare:
[self.keyArr sortUsingSelector:@selector(compare:)];
}
@end
//
// DataHandle.h
// UI11_Contact
//
// Created by dllo on 15/11/23.
// Copyright (c) 2015年 dllo. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface DataHandle : NSObject
+ (instancetype)shareData;
@property (nonatomic, retain) NSMutableDictionary *heroDic;
@property (nonatomic, retain) NSMutableArray *keyArr;
@end
//
// ContactViewController.m
// UI11_Contact
//
// Created by dllo on 15/11/23.
// Copyright (c) 2015年 dllo. All rights reserved.
//
#import "ContactViewController.h"
#import "Hero.h"
#import "AddViewController.h"
#import "ChineseToPinyin.h"
#import "DataHandle.h"
@interface ContactViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UITableView *tableV;
@end
@implementation ContactViewController
- (void)dealloc
{
[_tableV release];
[_tableV release];
[super dealloc];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableV reloadData];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = @"通讯录";
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(leftAction:)];
self.tableV = [[UITableView alloc] initWithFrame:self.view.frame];
self.tableV.delegate = self;
self.tableV.dataSource = self;
self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:self.tableV];
[_tableV release];
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 100)];
imageV.image = [UIImage imageNamed:@"1.png"];
self.tableV.tableHeaderView = imageV;
[imageV release];
}
- (void)leftAction:(UIBarButtonItem *)sender
{
AddViewController *AddVC = [[AddViewController alloc] init];
UINavigationController *AddNC = [[UINavigationController alloc] initWithRootViewController:AddVC];
[self presentViewController:AddNC animated:YES completion:^{
}];
[AddNC release];
[AddVC release];
}
/***************************** 移动 ****************************/
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
if (sourceIndexPath.section != proposedDestinationIndexPath.section) {
return sourceIndexPath;
} else {
return proposedDestinationIndexPath;
}
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSString *key = [[DataHandle shareData].keyArr objectAtIndex:sourceIndexPath.section];
NSMutableArray *arr = [[DataHandle shareData].heroDic objectForKeyedSubscript:key];
Hero *hero = [[arr objectAtIndex:sourceIndexPath.row] retain];
[arr removeObject:hero];
#warning 不跨区移动不用重新找key
[arr insertObject:hero atIndex:destinationIndexPath.row];
[hero release];
[self.tableV moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}
/***************************** 删除 ****************************/
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableV setEditing:editing animated:animated];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [[DataHandle shareData].keyArr objectAtIndex:indexPath.section];
NSMutableArray *arr = [[DataHandle shareData].heroDic objectForKey:key];
Hero *hero = [arr objectAtIndex:indexPath.row];
#warning 规范 - 判断一下编辑类型
if (UITableViewCellEditingStyleDelete == editingStyle) {
if (arr.count > 1) {
[arr removeObject:hero];
[self.tableV deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
} else {
[[DataHandle shareData].heroDic removeObjectForKey:key];
[[DataHandle shareData].keyArr removeObject:key];
[self.tableV deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationMiddle];
}
}
}
/***************************** 铺界面 ****************************/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *key = [[DataHandle shareData].keyArr objectAtIndex:section];
NSMutableArray *arr = [[DataHandle shareData].heroDic objectForKey:key];
return arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellStr = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellStr] autorelease];
}
NSString *key = [[DataHandle shareData].keyArr objectAtIndex:indexPath.section];
NSMutableArray *arr = [[DataHandle shareData].heroDic objectForKey:key];
Hero *hero = [arr objectAtIndex:indexPath.row];
cell.textLabel.text = hero.name;
cell.detailTextLabel.text = hero.phone;
cell.imageView.image = hero.headerImage;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// return self.keyArr.count;
return [DataHandle shareData].heroDic.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[DataHandle shareData].keyArr objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [DataHandle shareData].keyArr;
}
/***************************** 获取数据 ****************************/
- (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