UItableView编辑的步骤
2014-04-23 15:21
113 查看
self.navigationItem.rightBarButtonItem = self.editButtonItem;
第一步设置可编辑条件
- (void)setEditing:(BOOL)editing animated:(BOOL)animated { // 调用父类的(使其可以切换) [super setEditing:editing animated:YES]; // 获取表表视图的类型(定义成实例变量) UITableView *tableView = (UITableView *)self.view; // 设置可编辑状态 [tableView setEditing:editing animated:animated]; }
第二部设置进入编辑状态
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; }
第三部设置哪一个可以编辑
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { return UITableViewCellEditingStyleDelete; }if (indexPath.section == 1) { return UITableViewCellEditingStyleInsert; }else return UITableViewCellEditingStyleDelete; }
第四部设置可编辑状态的样式
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath { return @"删除"; }
第五部删除按钮标签名
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // 删除数据 // 拿到数据对象 NSString *key = [self.groupNames objectAtIndex:indexPath.section]; NSMutableArray *arry = [self.personDic objectForKey:key]; // 把删除,插入都写到begin和end中 [tableView beginUpdates]; if ([arry count] > 1 ) { [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade]; [arry removeObjectAtIndex:indexPath.row]; }else { // 删除字典中的数据 [self.personDic removeObjectForKey:key]; // 删分区时分区数量也得相应的减一 [self.groupNames removeObjectAtIndex:indexPath.section]; [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationRight]; } [tableView endUpdates]; }else if (editingStyle == UITableViewCellEditingStyleInsert) { // 添加到指定位置 // NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section]; Person *p = [Person personWithName:@"张三" sex:@"男" phoneNumber:@"32321323" address:@"大学西路" email:@"43454656@qq.com" age:@"32"]; // 通过key找出对应的数组信息 NSString *key = [self.groupNames objectAtIndex:indexPath.section]; // 通过键值找到对应的对象 NSMutableArray *arry = [self.personDic objectForKey:key]; // 在对应的数组中添加需要填加的对象 [arry insertObject:p atIndex:indexPath.row]; // 添加到当前行 [tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight]; } }第六步进行具体的编辑操作
下面介绍一下如何移动数据(数据存储是通过字典的方式存储的)步骤如下
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { NSLog(@"%@", sourceIndexPath); // 获取移动数据对象 NSString *sourceKey = [self.groupNames objectAtIndex:sourceIndexPath.section]; NSMutableArray *sourceGrop = [self.personDic objectForKey:sourceKey]; // 拿到数据对象 Person *p = [sourceGrop objectAtIndex:sourceIndexPath.row]; // 计数器加一次 [p retain]; // 拿到移动位置的数据位置 NSString *destinationkey = [self.groupNames objectAtIndex:destinationIndexPath.section]; // 拿到移动数据所在的数组 NSMutableArray *destinationkeyGrop = [self.personDic objectForKey:destinationkey]; // 移除原先的数据 [sourceGrop removeObject:p]; // 插入移动的后所在位置的数据 [destinationkeyGrop insertObject:p atIndex:destinationIndexPath.row]; // 释放数据 [p release]; }
// 分区移动 - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath { // 若果时同一个分区则交换 if (sourceIndexPath.section == proposedDestinationIndexPath.section) { return proposedDestinationIndexPath; }else // 不在同一个分区则不移动 return sourceIndexPath; }
相关文章推荐
- IOS开发学习笔记032-UITableView 的编辑模式
- 八点钟学院:由“虎扑体育app”的这个效果,来谈谈UITableView编辑模式
- UItableView 编辑
- IOS 表视图(UITableVIew)的使用方法(5)表视图的编辑功能(删除)
- UITableView,根据indexPath设置cell的编辑样式,删除cell ,增加cell &搜索条
- UITableView编辑单元格多个按钮(4)
- 【swift】UITableView 编辑模式多选(替换系统默认的图片)
- UITableView编辑模式可以点击Cell
- UITableView 表视图编辑
- iOS开发:UITableView编辑——cell的删除和移动
- UITableView编辑模式
- UITableView编辑模式
- UI:UITableView 编辑、cell重用机制
- UITableView 编辑总结
- UITableView实现删除移动编辑的样式(转)
- IOS 表视图(UITableVIew)的使用方法(8)表视图的编辑功能(多选)
- UITableView编辑模式
- UI - UITableView 编辑 、TableViewController
- UITableView编辑模式
- UITableView的编辑操作(移动&添加&删除-单选删除,或多选删除)