#pragma mark -----表视图的移动操作-----

//移动的第一步也是需要将表视图的编辑状态打开
//2、指定哪些行可以进行移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
//默认都可以移动
return YES;
} //3、移动完成之后要做什么事,怎么完成移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
//先记录原有位置下的模型数据
Student *student = _dataArray[sourceIndexPath.row];
[student retain]; //删除原位置下的模型数据
[_dataArray removeObjectAtIndex:sourceIndexPath.row]; //在新位置将记录的模型数据添加到数据数组中
[_dataArray insertObject:student atIndex:destinationIndexPath.row]; [student release];
} #pragma mark -----删除、添加数据----- //1、让将要执行删除、添加操作的表视图处于编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
//先执行父类中的这个方法。
[super setEditing:editing animated:animated]; //表视图执行此方法
[self.tableView setEditing:editing animated:animated];
} //2、指定表示图中哪些行可以处于编辑状态
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// if (indexPath.row % 2 == 0) {
// return YES;
// } else {
// return NO;
// }
//默认全部行都可以编辑
return YES;
} //3、指定编辑样式,到底是删除还是添加 delegate协议里面
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
//此方法如果不重写,默认是删除样式
// return UITableViewCellEditingStyleDelete;
return UITableViewCellEditingStyleInsert;
} //不管是删除还是添加,这个方法才是操作的核心方法,当点击删除、或者添加按钮时,需要做什么事情,怎样才能完成删除或者添加操作,全部在这个方法内部指定。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView beginUpdates];//表视图开始更新 if (editingStyle == UITableViewCellEditingStyleDelete) {
//将该位置下的单元格删除
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; //删除数据数组中与该单元格绑定的数据
[_dataArray removeObjectAtIndex:indexPath.row];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
Student *student = _dataArray[indexPath.row]; //构建一个位置信息
NSIndexPath *index = [NSIndexPath indexPathForRow: inSection:]; [tableView insertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationTop];
[_dataArray insertObject:student atIndex:index.row
];
} [tableView endUpdates];//表视图结束更新
) }

UITableView的添加、删除、移动操作的更多相关文章

  1. iOS仿网易新闻栏目拖动重排添加删除效果

    仿网易新闻栏目选择页面的基本效果,今天抽了点时间教大家如何实现UICollectionView拖动的效果! 其实实现起来并不复杂,这里只是基本的功能,没有实现细节上的修改,连UI都是丑丑的样子,随手画 ...

  2. UITableView划动删除的实现

    对于app应用来说,使用列表的形式展现数据非UITableView莫属.在熟练掌握了用UITableView展示数据以后,是不是也遇到了需要删除数据的需求?是不是觉得在一行数据上划动一下,然后出现一个 ...

  3. ios UITableView多选删除

    第一步, - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath ...

  4. iOS UITableView划动删除的实现

    标签:划动删除 iphone 滑动删除 ios UITableView 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainb ...

  5. WPF下的Richtextbox中实现表格合并,添加删除行列等功能

    .Net中已有现在的方法实现这些功能,不过可能是由于未完善,未把方法公开出来.只能用反射的方法去调用它. 详细信息可以查看.Net Framework 的源代码 http://referencesou ...

  6. 编辑 Ext 表格(一)——— 动态添加删除行列

    一.动态增删行 在 ext 表格中,动态添加行主要和表格绑定的 store 有关, 通过对 store 数据集进行添加或删除,就能实现表格行的动态添加删除.   (1) 动态添加表格的行  gridS ...

  7. Angular-表单动态添加删除

    angular本身不允许去操作DOM,在angular的角度来说,所有操作都以数据为核心,剩下的事情由angular来完成.所以说,想清楚问题的根源,解决起来也不是那么困难. 前提 那么,要做的这个添 ...

  8. 用Javascript动态添加删除HTML元素实例 (转载)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. [CentOS]添加删除用户

    摘要 在安装CentOS的时候,我们只设置了root,类似windows的超级管理员.当然我们在工作的时候,为了安全考虑,不可能对外开发root,一方面是从安全的角度,另一方面也是方便管理. 添加删除 ...

  10. 百度地图API示例之添加/删除工具条、比例尺控件

    代码 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" cont ...

随机推荐

  1. JavaScript 动态插入 CSS

    写组件时有时想把一些组件特性相关的 CSS 样式封装在 JS 里,这样更内聚,改起来方便.JS 动态插入 CSS 两个步骤就可以 创建一个 style 对象 使用 stylesheet 的 inser ...

  2. liunx作业一

    一.linux发行版 linux发行版是以linux为内核,包含了系统软件和应用软件.简化系统安装的工具.软件安装升级的集成管理器. 典型的linux发行版包括:linux内核,一些GNU程序库和工具 ...

  3. python3 linux下安装

    1.下载 https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz 2.安装 上传到linux服务器 #进入上传文件的目录 cd /app/pr ...

  4. 利用varnish做Discuz论坛的缓存服务器

    实验背景:公司有一台BBS服务器,用的是LNMP的架构搭建的.正好手头有一台空闲的虚拟机,于是想着给BBS前端加一台缓存服务器.于是选定了varnish,搜了很多教程,跌跌撞撞的完成了配置.这其中很多 ...

  5. EF继承关系映射

    继承映射策略的三种策略 There are following three different approaches to represent an inheritance hierarchy in ...

  6. python爬虫学习 —— 总目录

    开篇 作为一个C党,接触python之后学习了爬虫. 和AC算法题的快感类似,从网络上爬取各种数据也很有意思. 准备写一系列文章,整理一下学习历程,也给后来者提供一点便利. 我是目录 听说你叫爬虫 - ...

  7. MBTI-性格测试

  8. SQLMAP参数介绍

    转自:http://zhan.renren.com/bugpower?gid=3602888498044629629&checked=true SQLMAP参数介绍 sqlmap的使用方式:p ...

  9. 非常棒的Visual Studo调试插件:OzCode

    非常棒的Visual Studo调试插件:OzCode 周银辉 视频介绍 http://channel9.msdn.com/Shows/Visual-Studio-Toolbox/OzCode 官方网 ...

  10. 20145208 《Java程序设计》第0周学习总结

    20145208 <Java程序设计>第0周学习总结 阅读心得 读了老师推荐的几个文章,虽然第四个文章"为什么一定要自学"报告资源不存在而无法阅读,其他的三篇文章都言之 ...