tableViewCell添加plist删除和移动联系人
来源:互联网 发布:手机怎么抢网络 编辑:程序博客网 时间:2024/05/19 13:23
通讯录的添加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];}
0 0
- tableViewCell添加plist删除和移动联系人
- TableViewCell添加和删除
- iOS开发:通讯录之联系人添加、移动、删除
- iPhone 添加删除联系人
- 什么是“常用联系人”,如何添加\删除联系人?
- tableviewcell添加多个按钮删除事件
- Android批量添加删除联系人
- ios UITableView的移动、添加和删除
- 添加和读取联系人
- iOS tableViewCell 的移动操作 (添加长按手势)
- Swift - 添加、修改、删除通讯录联系人
- 查询和添加系统联系人
- 添加和取消关注联系人
- Android管理联系人(包含添加,查询,修改和删除;以及不同版本区别)
- android 数据库 SQLiteOpenHelper和ContentProvider学习笔记---添加修改删除数据之联系人(二)
- iOS tableviewCell的多行选择删除和全选删除
- iOS tableviewCell的多行选择删除和全选删除
- jQuery表格操作添加行、删除行和动态移动
- mysql格式化操作
- hdu 2516 取石子游戏(博弈,找规律)
- 黑马程序员-----2015.6.12java笔记
- AsyncHttpClient(上传图片到服务器)
- STM32的IO口的8种配置
- tableViewCell添加plist删除和移动联系人
- 黑马程序员----2015.6.12java笔记---进制--成员变量--匿名函数--构造函数
- spring 事务隔离级别及传播行为
- Mac OS X安装Maven
- java如何获取AD域用户信息?
- Animation
- eclipse java 和 普通文件中,注释汉字字体问题。
- collection、iterator和map的解析
- porting mini_httpd for arm