标签:
// // RootViewController.m // UI_tableView编辑 // // Created by Hunter on 15/12/5. // Copyright © 2015年 HaiTeng. All rights reserved. // #import "RootViewController.h" #import "SuperManModel.h" @interface RootViewController ()<UITableViewDelegate,UITableViewDataSource> @property(nonatomic,strong)UITableView *tableView; @property(nonatomic,strong)NSMutableDictionary *dataDic; //数据,字典 @property(nonatomic,strong)NSMutableArray *allKeys; //所有key @property(nonatomic,strong)NSMutableDictionary *dic; //接收数据,字典 @end @implementation RootViewController NSString *cell_id = @"cell_id"; #pragma mark 处理数据 - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if ([super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { _dic = [NSMutableDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"BJS151043" ofType:@"plist"]]; self.allKeys = [_dic allKeys].mutableCopy; [self.allKeys sortUsingSelector:@selector(compare:)]; _dataDic = [NSMutableDictionary dictionary]; for (NSString *key in self.allKeys) { NSMutableArray *array = [_dic objectForKey:key]; NSMutableArray *arra = [NSMutableArray array]; for (NSDictionary *di in array) { SuperManModel *model = [[SuperManModel alloc] init]; [model setValuesForKeysWithDictionary:di]; [arra addObject:model]; } [_dataDic setObject:arra forKey:key]; } NSLog(@"%@",_dataDic); } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor redColor]; self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:0]; self.tableView.rowHeight = 60; self.tableView.delegate = self; self.tableView.dataSource = self; [self.view addSubview:self.tableView]; // 注册cell [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cell_id]; //右按钮 UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)]; self.navigationItem.rightBarButtonItem = right; //系统提供的编辑按钮 // self.navigationItem.rightBarButtonItem = self.editButtonItem; // self.editButtonItem.title = @"编辑"; } #pragma mark 代理方法 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { //通过key值,找Value值 return [self.dataDic[_allKeys[section]] count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id]; SuperManModel *model = self.dataDic[_allKeys [indexPath.section]][indexPath.row]; cell.textLabel.text = model.name; cell.imageView.image = [UIImage imageNamed:model.picture]; return cell; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.allKeys.count; } - (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return _allKeys; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return _allKeys[section]; } #pragma mark TableView编辑 #pragma rightAction事件 //第一步:是页面处于可编辑状态 - (void)rightAction:(UIBarButtonItem *)sender { //设置当前页面可以被编辑 //当点击的时候,页面应该处于可编辑状态,并且按钮文字变成"完成" if ([sender.title isEqualToString:@"编辑"]) { sender.title = @"完成"; [_tableView setEditing:YES animated:YES]; //让按钮处于可编辑状态 }else{ //当点击完成的时候,应该让页面处于不可编辑状态,并且按钮文字显示为"编辑" sender.title = @"编辑"; [_tableView setEditing:NO animated:YES]; } } //第二步:指定哪些行可以被编辑 -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 2) { return NO; } //(这里我让第三组,不可编辑做下演示) return YES; } //第三步:根据路径指定tableView的编辑样式 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { return UITableViewCellEditingStyleInsert; } //这里我设置第一行为增加,其他的为删除(做下演示) return UITableViewCellEditingStyleDelete; } //第四步:根据编辑风格,完成编辑(先修改数据源,再修改UI) - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { //判断 if (editingStyle == UITableViewCellEditingStyleDelete) { //删除 NSMutableArray *array = _dataDic[_allKeys[indexPath.section]];//找到相应数组 if (array.count == 1) { //删除数据 // [_dataDic removeObjectForKey:_allKeys[indexPath.section]]; [_allKeys removeObjectAtIndex:indexPath.section]; //删除UI [_tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationTop]; }else{ //删除数据 [array removeObjectAtIndex:indexPath.row]; //删除UI [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop]; } }else{ //增加 NSDictionary *diccc = [NSDictionary dictionaryWithObjectsAndKeys:@"熊大",@"name",@"男",@"gender",@"玩",@"hobby",@"11111",@"phoneNumber",@22,@"age",@"1.png",@"picture", nil]; SuperManModel *sm = [[SuperManModel alloc] init]; [sm setValuesForKeysWithDictionary:diccc]; // 找分组 NSMutableArray *array = _dataDic[_allKeys[indexPath.section]]; //添加元素 [array insertObject:sm atIndex:indexPath.row + 1];//(添加到这一行下面去) //更新UI NSIndexPath *newIndex = [NSIndexPath indexPathForRow:indexPath.row +1 inSection:indexPath.section]; [_tableView insertRowsAtIndexPaths:@[newIndex] withRowAnimation:UITableViewRowAnimationLeft]; } } #pragma tableView移动 /* //第一步:是页面处于,可编辑状态 - (void)setEditing:(BOOL)editing animated:(BOOL)animated { [super setEditing:editing animated:animated]; [self.tableView setEditing:editing animated:animated]; //编辑按钮文字 self.navigationItem.rightBarButtonItem.title = editing ? @"完成" : @"编辑"; } //第二步:指定tableView哪些行可以移动 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } 动数据 NSMutable //第三步:移动完成 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { //先获取移Array *array = _dataDic[_allKeys[sourceIndexPath.section]]; //获取移动数据中的对象 NSDictionary *dic = [array objectAtIndex:sourceIndexPath.row]; //先删除,再添加 //删除 [array removeObjectAtIndex:sourceIndexPath.row]; //添加 [array insertObject:dic atIndex:destinationIndexPath.row]; } //第四步:tableView 检测跨区移动 - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath { if (proposedDestinationIndexPath.section == sourceIndexPath.section) { return proposedDestinationIndexPath; } return sourceIndexPath; } */ /* #pragma Mark cell 编辑自定义操作 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { //这个方法,写这不用管就OK } - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { NSLog(@"删除"); NSMutableArray *array = _dataDic[_allKeys[indexPath.section]]; if (array.count == 1) { [_dataDic removeObjectForKey:_allKeys[indexPath.section]]; [_allKeys removeObjectAtIndex:indexPath.section]; [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationTop]; }else{ [array removeObjectAtIndex:indexPath.row]; [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop]; } }]; UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"增加" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { NSLog(@"增加"); NSDictionary *diccc = [NSDictionary dictionaryWithObjectsAndKeys:@"熊大",@"name",@"男",@"gender",@"玩",@"hobby",@"11111",@"phoneNumber",@22,@"age",@"1.png",@"picture", nil]; SuperManModel *mode = [[SuperManModel alloc] init]; [mode setValuesForKeysWithDictionary:diccc]; NSMutableArray *array = _dataDic[_allKeys[indexPath.section]]; [array insertObject:mode atIndex:indexPath.row]; [self. tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; }]; return @[action1,action2]; } */ @end
TableView Editor and delete add Test
标签:
原文地址:http://www.cnblogs.com/HaiTeng/p/5510752.html