UItableView编辑的步骤
来源:互联网 发布:飞行员和程序员 编辑:程序博客网 时间:2024/04/29 22:22
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;}
0 0
- UItableView编辑的步骤
- UITableView的数据编辑
- UITableVIew的编辑状态
- UITableView的编辑模式
- UITableView的编辑
- UITableView 的编辑
- UITableView的编辑
- UITableView的编辑
- UITableView的编辑模式
- UITableView的编辑操作
- 禁止编辑UITableView的Cell
- UITableVIew的一些编辑属性
- UITableView表视图的编辑
- UITableView的常用编辑操作
- iOS UITableView的编辑方法
- UITableView的编辑功能,UICollectionView
- 31. UITableView的编辑模式
- UITableVIew的一些编辑属性
- 函数模板和类模板 模版特化
- 用递归实现代码过滤
- 决定重新开始学习C#啦
- Nginx的Rewrite重写规则
- 关于WPF Style 的理解
- UItableView编辑的步骤
- VS2010/MFC编程入门之五(对话框:创建对话框模板和修改对话框属性)
- 的女子不由觉得有些一路走来
- 花开人间四月天
- RadioButtonList选项加入ListBox
- Android开发学习之Toasting应用与实例
- Direct3D 10教程0:Win32编程基础
- 姑娘听到这老魔头的心中大为失
- 办公软件 office2007,完整版 正式版,破解版