UITableViewCell的移动、插入与删除
来源:互联网 发布:手机淘宝店铺模板复制 编辑:程序博客网 时间:2024/05/16 06:01
当我们的工程中需要动态插入或者删除UITableViewCell时,我们该如何做呢?
如果要实现这些操作,前提要实现移动单元格就需要把单元格的编辑属性设置为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
0 0
- UITableViewCell的移动、插入与删除
- UITableViewCell的标记、移动、删除、插入
- UITableViewCell的标记、移动、删除、插入
- UITableViewCell的标记、移动、删除、插入
- UITableViewCell的标记、移动、删除、插入
- UITableViewCell的标记、移动、删除、插入
- UITableViewCell的标记、移动、删除、插入
- UITableViewCell的标记、移动、删除、插入
- UITableViewCell的标记、移动、删除、插入
- UITableViewCell的标记、移动、删除、插入
- UITableViewCell的标记、移动、删除、插入
- (新手上路)UITableVIewCell移动,标记,插入,删除,多行删除
- UITableViewCell滑动删除及移动
- UITableViewCell滑动删除及移动
- UITableViewCell滑动删除及移动
- UITableViewCell滑动删除及移动
- UITableViewCell滑动删除及移动
- UITableViewCell的操作(增加cell,删除cell,插入cell)
- 数据库中多条数据,Java后台进行组装并显示到页面,Java后台该怎么组装
- 解决Eclipse建立Maven项目后无法建立src/main/java资源文件夹的办法
- 如何将数据库连接字符串保存在配置文件中并读取
- Web 服务编程,REST 与 SOAP
- intel edison固件更新
- UITableViewCell的移动、插入与删除
- 【RMQ-ST算法】hihocoder1068
- Ubuntu 14.04 安装 Sublime Text 3
- 键盘上的快捷键知多少
- 滴滴快的笔试题:最长和为零的子数组
- c++ set
- org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identi
- 十种经典排序算法
- SQL Server数据库删除行问题