通讯录的添加plist删除和移动联系人
#import "RootViewController.h"
#import "RootView.h"
#import "Person.h"
@interface RootViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) RootView *rootView;
// 创建可变字典,用来存储全部数据
@property (nonatomic, retain) NSMutableDictionary *allDataDict;
@end
@implementation RootViewController
- (void)loadView
{
self.rootView = [[[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
self.view = _rootView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 加载数据
[self loadData];
// 设置数据源
_rootView.tableView.dataSource = self;
_rootView.tableView.delegate = self;
// 导航栏右上角添加编辑按钮
self.navigationItem.rightBarButtonItem = ({
[[[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(editBarButtonItemAction:)] autorelease];
});
}
#pragma mark - 编辑按钮事件
- (void)editBarButtonItemAction:(UIBarButtonItem *)sender
{
// 设置状态(取反)
[_rootView.tableView setEditing:!_rootView.tableView.isEditing animated:YES];
// 根据状态,修改文字
if (_rootView.tableView.isEditing == NO) {
sender.title = @"编辑";
} else {
sender.title = @"完成";
}
}
#pragma mark 此方法处理数据
- (void)loadData
{
// 1.读取plist文件中的内容
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Class0409AllStudents" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
self.allDataDict = [NSMutableDictionary dictionary];
// 2.遍历字典中的所有的key值
for (NSString *key in dict.allKeys) {
// 3.根据key值获取对应的数组
NSArray *array = dict[key];
// 可变数组,用来存放转好的Person对象
NSMutableArray *mutableArray = [NSMutableArray array];
// 4.遍历数组中的内容
for (NSDictionary *item in array) {
// 5.将字典转为Person对象
Person *p = [Person new];
[p setValuesForKeysWithDictionary:item];
// 将模型存放到可变数组中
[mutableArray addObject:p];
[p release];
}
// 6.将可变数组和key值对应起来,放到大字典中
[_allDataDict setObject:mutableArray forKey:key];
}
// NSLog(@"%@", _allDataDict);
}
#pragma mark - UITableViewDataSources Delegate
#pragma mark 设置分组个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _allDataDict.count;
}
#pragma mark 设置分组的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// NSString *key = _allDataDict.allKeys[section];
//
// NSArray *array = _allDataDict[key];
//
// return array.count;
return [_allDataDict.allValues[section] count];
}
#pragma mark 设置每行上显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];
}
// Person *p = _allDataDict[_allDataDict.allKeys[indexPath.section]][indexPath.row];
NSString *key = _allDataDict.allKeys[indexPath.section];
NSMutableArray *array = _allDataDict[key];
Person *p = array[indexPath.row];
cell.imageView.image = [UIImage imageNamed:p.headImageName];
cell.textLabel.text = p.name;
cell.detailTextLabel.text = p.phoneNumber;
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
#pragma mark 快速索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return _allDataDict.allKeys;
}
#pragma mark 设置头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return _allDataDict.allKeys[section];
}
#pragma mark 设置编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
#pragma mark 编辑过程
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
/*
// 先在大字典中删除数据
NSString *key = _allDataDict.allKeys[indexPath.section];
NSMutableArray *mutableArray = _allDataDict[key];
[mutableArray removeObjectAtIndex:indexPath.row];
// 在页面上删除cell
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
// 判断剩余数组人数,并决定是否删除该分组
if (mutableArray.count == 0) {
[_allDataDict removeObjectForKey:key];
NSIndexSet *set = [[[NSIndexSet alloc] initWithIndex:indexPath.section] autorelease];
[tableView deleteSections:set withRowAnimation:UITableViewRowAnimationLeft];
}
*/
NSString *key = _allDataDict.allKeys[indexPath.section];
NSMutableArray *mutableArray = _allDataDict[key];
if (mutableArray.count == 1) {
// 删除了分组
[_allDataDict removeObjectForKey:key];
// 更新页面
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationLeft];
} else {
// 从数组中删除元素
[mutableArray removeObjectAtIndex:indexPath.row];
// 更新页面
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
#pragma mark 设置cell可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
#pragma mark 处理数据移动的方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// 根据section获取key值
NSString *key = _allDataDict.allKeys[sourceIndexPath.section];
// 根据key值,获取可变数组
NSMutableArray *mutableArray = _allDataDict[key];
// 根据row,获取要操作的Perosn对象
Person *p = [mutableArray[sourceIndexPath.row] retain];
// 根据对象删除对象
[mutableArray removeObject:p];
// 在新的位置插入对象
[mutableArray insertObject:p atIndex:destinationIndexPath.row];
[p release];
}
#pragma mark - 设置禁止跨分区移动
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
// 如果目标分区和原分区一致,表示在当前分区移动
if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
// 返回目标位置
return proposedDestinationIndexPath;
}
return sourceIndexPath;
}
- (void)dealloc
{
[_rootView release];
[_allDataDict release];
[super dealloc];
}