UItableView中编辑,删除,移动学习笔记
来源:互联网 发布:淘宝男装店铺排行粉丝 编辑:程序博客网 时间:2024/05/29 04:01
//// RootViewController.m// UI10TableViewController//// Created by lanou3g on 15/8/20.// Copyright (c) 2015年 Yuxiang. All rights reserved.//#import "RootViewController.h"#import "Person.h"@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>@property(nonatomic,retain)UITableView * tableView;@property(nonatomic,retain)NSMutableDictionary * dataDictionary;@property(nonatomic,retain)NSMutableArray * dataArray;@property(nonatomic,assign)BOOL isEdit;@end@implementation RootViewControllerstatic NSString * identifier = @"My";-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ if(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]){ NSString * path = [[NSBundle mainBundle]pathForResource:@"StudentInformation.plist" ofType:nil]; //最外层字典 _dataDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:path]; //所有key值(分组名) _dataArray = [NSMutableArray arrayWithArray:[_dataDictionary allKeys]]; //排序 [_dataArray sortUsingSelector:@selector(compare:)]; NSLog(@"%ld",_dataArray.count); //添加编辑按钮 UIBarButtonItem * left= [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(edittableView:)]; self.navigationItem.leftBarButtonItem = left; //添加编辑按钮 //系统为我们提供一个编辑按钮,同时也提供了编辑方法,我们可以不用重写属性和方法 self.navigationItem.rightBarButtonItem= self.editButtonItem; //默认显示edit self.editButtonItem.title = @"编辑"; } return self;}#pragma mark -----tableView编辑//(系统给的方法)-(void)setEditing:(BOOL)editing animated:(BOOL)animated{ _isEdit = NO;//左右按钮分开使用 //向父类发送消息 //在OC中,重写父类方法的时候,必须现象父类发送super消息,尤其是系统 [super setEditing:editing animated:animated]; [_tableView setEditing:editing animated:animated];//系统方法 //换title self.navigationItem.rightBarButtonItem.title = editing?@"完成":@"编辑"; }#pragma mark --------tableView编辑//第一步:使用tableVIew处于编辑状态(自定义的)-(void)edittableView:(UIBarButtonItem *)sender{ _isEdit = YES; //当按钮显示编辑的时候,我破门操作这个按钮 //1.tableView处于编辑状态 //2.按钮先显示为"完成" //当按钮显示为完成状态时候,我们操作这个按钮 //1.让tableView处于非编辑状态 //2.按钮显示为"编辑" if([sender.title isEqualToString:@"编辑"]){ sender.title = @"完成"; [_tableView setEditing:YES animated:YES]; sender.title = @"完成"; } else{ [_tableView setEditing:NO animated:YES]; sender.title = @"编辑"; }}//第二步:指定哪些路径可以被编辑//-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{//// if(indexPath.row == 0){//// return NO;//// }// return YES;//}//第三步:指定编辑样式(默认返回删除)//-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{//// UITableViewCellEditingStyleNone,//// UITableViewCellEditingStyleDelete,//// UITableViewCellEditingStyleInsert// //// if(indexPath.section < 3){//// return UITableViewCellEditingStyleDelete;//// }//////// else{//// return UITableViewCellEditingStyleInsert;//// }// if(_isEdit == YES){// return UITableViewCellEditingStyleDelete;// }// else{// return UITableViewCellEditingStyleInsert;// }// //}//第四步:完成编辑//- (void)tableView:(UITableView *)tableView commitEditingStyle://(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath://(NSIndexPath *)indexPath{//#warning -在进行删除数据的时候,我们要先删除数据,在删除UI// if(editingStyle == UITableViewCellEditingStyleDelete)// {// // NSLog(@"删除");// //1.操作数据// //(全组的人)// NSMutableArray * mutArray = [_dataDictionary objectForKey:_dataArray[indexPath.section]];// //当分组里面只有一个人的时候,我们删除完之后,需要删除其所在分组// if(mutArray.count == 1){// //移除整组// [_dataDictionary removeObjectForKey:_dataArray[indexPath.section]];// [_dataArray removeObjectAtIndex:indexPath.section];// //更新UI// NSIndexSet * set = [NSIndexSet indexSetWithIndex:indexPath.section];// //获取删除后的新UI// [_tableView deleteSections:set withRowAnimation:UITableViewRowAnimationTop];// }// else{// // //具体人// [mutArray removeObjectAtIndex:indexPath.row];// //2.操作UI// [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];// }// // }// else if (editingStyle == UITableViewCellEditingStyleInsert ){// NSLog(@"添加");// //创建字典// NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:@"东东哥",@"name",@"男",@"gender",@"东东哥.png",@"image", nil];// //添加数据操作,(先找路径)// //找到对应的分区// NSMutableArray * array = [_dataDictionary objectForKey:_dataArray[indexPath.section]];// //加入数组中,+1是加载点击的联系人后面// [array insertObject:dic atIndex:indexPath.row + 1];// // //操作UI// //创建添加的位置// NSIndexPath * newPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];// //插入// [_tableView insertRowsAtIndexPaths:@[newPath] withRowAnimation:UITableViewRowAnimationMiddle];// }//}#pragma mark --------以上是tableView编辑#pragma mark --------以下是tableView移动//第二个方法-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{ return YES;}//第三个方法- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath{ //获取移动的数据(sourceIndexPath指原地址) NSMutableArray * array = [_dataDictionary objectForKey:_dataArray[sourceIndexPath.section]]; NSDictionary * dic = [array objectAtIndex:sourceIndexPath.row];//复制原来数据 //删除原数据 [array removeObjectAtIndex:sourceIndexPath.row]; //再插入 [array insertObject:dic atIndex:destinationIndexPath.row]; }//跨行移动//检测跨行移动方法-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{ //如果在一个分区我们就可以让他随便移动 if(sourceIndexPath.section == proposedDestinationIndexPath.section){ return proposedDestinationIndexPath; } else{ //否则,原路径回去 return sourceIndexPath; } }#pragma mark --------以下是tableView移动- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //添加tableView [self addTableView]; NSLog(@"%d",_isEdit);}#pragma mark ----tableView dataSouce//多少分区-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return _dataArray.count;}//-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ //取出当前分区的标题 NSString * key = _dataArray[section]; //取出当前分区的row的个数 NSArray * array = _dataDictionary[key]; return array.count;}//设置分区头-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return _dataArray[section];}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier]; //使用判断创建cell/* if(cell == nil){ cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier; }*/ //取出分区对应的数组 NSArray * array = [_dataDictionary objectForKey:_dataArray[indexPath.section]]; NSLog(@"82-------%@",array); //取出具体的字典(行) NSDictionary * dictionary = array[indexPath.row]; NSLog(@"85-------%@",dictionary); //完成model赋值 Person * per = [Person new]; //kvc [per setValuesForKeysWithDictionary:dictionary]; cell.textLabel.text = per.name; cell.imageView.image = [UIImage imageNamed:per.image]; return cell;}-(void)addTableView{ self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.rowHeight = 60; //注册,创建cell不在使用判断,直接可以使用iOS_6以后的新方法 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier]; [self.view addSubview:_tableView];}-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return _dataArray;}- (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
1 0
- UItableView中编辑,删除,移动学习笔记
- UITableView实现删除移动编辑的样式
- UITableView 编辑状态(删除、添加、移动)
- UITableView-编辑模式(多选移动删除)
- UIday1001:UITableView 2 编辑(添加、删除)、移动、UITableViewController
- UITableView滑动删除学习笔记
- UITableView(二编辑移动)
- UITableView 添加,删除,编辑
- UITableView(二编辑删除)
- UITableView的编辑操作(移动&添加&删除-单选删除,或多选删除)
- IOS UITableView编辑模式中,只有移动功能
- UITableView 编辑(增删移动)
- UITableView 自带编辑删除
- 【精】表格(UITableView)总结(4):编辑(增加、删除、移动)
- iOS开发:UITableView编辑——cell的删除和移动
- 28.UITableView表视图第三章(移动和编辑删除和添加)
- iOS Programming 学习笔记 - 04 UITableView编辑模式
- iOS学习笔记-029.UITableView的编辑模式
- 时分秒选择控件
- POJ 2761(求区间第k小值)
- Android USB相关广播的使用
- C#窗体中根据两点坐标绘制直线
- subst
- UItableView中编辑,删除,移动学习笔记
- 【LeetCode】(94)Binary Tree Inorder Traversal(Easy)
- 架构设计常见安全误区
- spring+activemq 发送10W消息报端口被占用的异常分析以及topic持久化订阅
- android edittext 拼写检查
- CocoaPods用法
- Android翻页效果原理实现之曲线的实现 分类: Custom My Love
- 黑马程序员-----集合类
- 微信演示制作软件易企秀的上手体验(图文)