UITableViewCell的移动、插入与删除
2015-09-25 21:48
465 查看
当我们的工程中需要动态插入或者删除UITableViewCell时,我们该如何做呢?
如果要实现这些操作,前提要实现移动单元格就需要把单元格的编辑属性设置为YES,有两种方式,第一种是改变tableview的属性:
1.移动
2.删除
首先是判断(UITableViewCellEditingStyle)editingStyle
3.插入
实现方法和删除方法相同,首先还是返回单元格编辑风格
4.推荐一种已经实现好的可以滑动出现效果的:SWTableViewCell
如果要实现这些操作,前提要实现移动单元格就需要把单元格的编辑属性设置为YES,有两种方式,第一种是改变tableview的属性:
[tableView setEditing:YES animated:YES];,第二种通过代理的方式:
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ return YES; }
1.移动
//返回YES,表示支持单元格的移动 -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { return YES; }
//单元格返回的编辑风格,包括删除 添加 和 默认 和不可编辑三种风格 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleInsert; }
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { // 需要的移动行 NSInteger fromRow = [sourceIndexPath row]; // 获取移动某处的位置 NSInteger toRow = [destinationIndexPath row]; // 从数组中读取需要移动行的数据 id object = [self.listData objectAtIndex:fromRow]; // 在数组中移动需要移动的行的数据 [self.listData removeObjectAtIndex:fromRow]; // 把需要移动的单元格数据在数组中,移动到想要移动的数据前面 [self.listData insertObject:object atIndex:toRow]; }
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ NSInteger from=sourceIndexPath.row; NSInteger to=destinationIndexPath.row; id obj=[self.items objectAtIndex:from]; [self.items removeObjectAtIndex:from]; [self.items insertObject:obj atIndex:to]; }
2.删除
首先是判断(UITableViewCellEditingStyle)editingStyle
//单元格返回的编辑风格,包括删除 添加 和 默认 和不可编辑三种风格 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete; }
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle==UITableViewCellEditingStyleDelete) { // 获取选中删除行索引值 NSInteger row = [indexPath row]; // 通过获取的索引值删除数组中的值 [self.listData removeObjectAtIndex:row]; // 删除单元格的某一行时,在用动画效果实现删除过程 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } }
3.插入
实现方法和删除方法相同,首先还是返回单元格编辑风格
/单元格返回的编辑风格,包括删除 添加 和 默认 和不可编辑三种风格 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleInsert; }
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle==UITableViewCellEditingStyleDelete) { // 获取选中删除行索引值 NSInteger row = [indexPath row]; // 通过获取的索引值删除数组中的值 [self.listData removeObjectAtIndex:row]; // 删除单元格的某一行时,在用动画效果实现删除过程 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } if(editingStyle==UITableViewCellEditingStyleInsert) { i=i+1; NSInteger row = [indexPath row]; NSArray *insertIndexPath = [NSArray arrayWithObjects:indexPath, nil]; NSString *mes = [NSString stringWithFormat:@"添加的第%d行",i]; // 添加单元行的设置的标题 [self.listData insertObject:mes atIndex:row]; [tableView insertRowsAtIndexPaths:insertIndexPath withRowAnimation:UITableViewRowAnimationRight]; } }
4.推荐一种已经实现好的可以滑动出现效果的:SWTableViewCell
相关文章推荐
- UI高度可定制化KxMenu弹出菜单
- Android UI系列-----EditText和AutoCompleteTextView
- 黑马程序员—java技术blog—第十二篇java基础应用GUI模拟登陆注册
- UIHelp自动获取孙子节点的函数
- 云方案,依托H3C彩虹云存储架构,结合UIA统一认证系统,实现了用户数据的集中存储和管理
- n queens
- POJ 1679 The Unique MST (次小生成树)
- WAJUEJI which home strong! 1100 (广搜)
- quick-union【连通图】
- quick-find【连通图】
- GUI
- 【UIKit-125-2】#import <UIKit/UIViewController.h>
- Uncaught SyntaxError: Failed to execute 'querySelectorAll' on 'Element': '#00' is not a valid select
- 项目:片刻 DAY1 搭建框架结构
- UITableView默认选中第一个cell
- OC基础-NSValue类簇-将复杂数据类型保存为对象
- 【UIKit-125-1】#import <UIKit/UIViewController.h>
- HDOJ 题目3415 Max Sum of Max-K-sub-sequence(单调队列求区间和最大值)
- iOS:下拉刷新控件UIRefreshControl的详解
- 初识duilib