UITableView的编辑模式
UITableView可以分普通模式和Editing模式两种,这里我们着重讨论Editing模式,Editing模式中又分三种操作:Insert、Delete、 Reallocted。Insert和Delete针对数据源内容的修改,而Reallocated是针对数据源位置的修改。下面分别讨论。
一、Insert Or Delete
当UITableView接收到 setEditing:animated:时,它会将同样的消息转发给每一个可见行,大致会经历如下步骤,引用至官方:

The table view invokes the tableView:canEditRowAtIndexPath: method if its data source implements it. This method allows the application to exclude rows in the table view from being edited even when their cell’s editingStyle property indicates otherwise. Most applications do not need to implement this method.
The table view invokes the tableView:editingStyleForRowAtIndexPath: method if its delegate implements it. This method allows the application to specify a row’s editing style and thus the editing control that the row displays.
At this point, the table view is fully in editing mode. It displays the insertion or deletion control for each eligible row.
The user taps an editing control (either the deletion control or the insertion control). If he or she taps a deletion control, a Delete button is displayed on the row. The user then taps that button to confirm the deletion.
The table view sends the tableView:commitEditingStyle:forRowAtIndexPath: message to the data source. Although this protocol method is marked as optional, the data source must implement it if it wants to insert or delete a row. It must do two things:
Send deleteRowsAtIndexPaths:withRowAnimation: or insertRowsAtIndexPaths:withRowAnimation: to the table view to direct it to adjust its presentation.
Update the corresponding data-model array by either deleting the referenced item from the array or adding an item to the array.
总结起来:
1、外部控件或委托向UITableView发送setEditing:方法,传入YES表示将要针对UITableView执行编辑操作
2、UITableView向其中的每一个UITableViewCell发送setEditing:方法,传入的参数取决于tableView:canEditRowAtIndexPath:
3、如果这一行确定进入EditingMode,UITableView向代理询问这一行的编辑样式,发送消息tableView:editingStyleForRowAtIndexPath:
4、根据EditingStyle显示相应的按钮,当用户点击那个editing按钮后,如果是删除按钮,系统会在右边显示该按钮以备再次向用户确认是否删除,当用户点击确认删除后,操作完成;如果是新增按钮,操作直接完成。
5、UITableView向代理发送 tableView:commitEditingStyle:forRowAtIndexPath:消息,告知代理用户已经完成操作,此处代码可以根据相应的业务逻辑来对数据源执行操作,操作完毕后调用UITableView的相关方法来更新cell。例如发送deleteRowsAtIndexPaths:withRowAnimation:消息告诉UITableView删除指定索引的行,发送insertRowsAtIndexPaths:withRowAnimation:消息告诉UITableView在指定位置新增行。
适用场景:
1、可以在用户点击加载更多时,批量新增多行。
二、Reallocted
Reallocted也是编辑模式下的一种,当UITableView接收到 setEditing:animated:方法时,也有机会进入Reallocated模式。这里引用官方的图片以及步骤:

When the table view receives the setEditing:animated: message, it resends the same message to the cell objects corresponding to its visible rows. After that, the sequence of messages is as follows:
The table view sends a tableView:canMoveRowAtIndexPath: message to its data source (if it implements the method). In this method the delegate may selectively exclude certain rows from showing the reordering control.
The user drags a row by its reordering control up or down the table view. As the dragged row hovers over a part of the table view, the underlying row slides downward to show where the destination would be.
Every time the dragged row is over a destination, the table view sends tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath: to its delegate (if it implements the method). In this method the delegate may reject the current destination for the dragged row and specify an alternative one.
The table view sends tableView:moveRowAtIndexPath:toIndexPath: to its data source (if it implements the method). In this method the data source updates the data-model array that is the source of items for the table view, moving the item to a different location in the array.
总结:
1、外部控件或委托向UITableView发送setEditing:方法,传入YES表示将要针对UITableView执行编辑操作
2、UITableView向其中的每一个UITableViewCell发送setEditing:方法,传入的参数取决于tableView:canMoveRowAtIndexPath:
3、用户通过reordering control拖拽某一行A,当拖拽到这个Table View的另一行B的上方时,B会被挤到下方显示。
4、每当拖拽到一个目标上方时,UITableView会发送tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:消息给代理,询问是否要这么移动,有没有需要修正的,此处可以用于重新定位,例如用户拖动到外部区域去了,下面会有个示例代码1.1。
5、UITableView最终向数据源DataSource对象发送 tableView:moveRowAtIndexPath:toIndexPath:消息,确定最终移动到哪里,通知它更改数据源并且修改更新UITableView中Cell的位置。
- (NSIndexPath *)tableView:(UITableView *)tableViewtargetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPathtoProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {NSDictionary *section = [data objectAtIndex:sourceIndexPath.section];NSUInteger sectionCount = [[section valueForKey:@"content"] count];if (sourceIndexPath.section != proposedDestinationIndexPath.section) {NSUInteger rowInSourceSection =(sourceIndexPath.section > proposedDestinationIndexPath.section) ?0 : sectionCount - 1;return [NSIndexPath indexPathForRow:rowInSourceSection inSection:sourceIndexPath.section];} else if (proposedDestinationIndexPath.row >= sectionCount) {return [NSIndexPath indexPathForRow:sectionCount - 1 inSection:sourceIndexPath.section];}// Allow the proposed destination.return proposedDestinationIndexPath;}
代码1.1
UITableView的编辑模式的更多相关文章
- 让UITableView进入编辑模式
1.UITableView对象有一个editing属性,设为YES时,该对象会进入编辑模式(editing mode).表格视图进入编辑模式后,用户可以管理表格中得行,如改变行的排列顺序.增加行或删除 ...
- 应用程序之UITableView的编辑模式
cell分层结构 效果展示 代码实现 一.cell的分层结构 二.效果展示 三.代码实现 // // ViewController.m // 01-TableView的删除实现 // // Creat ...
- UITableView编辑模式大全解
1.UITableView 的编辑模式 进入编辑模式 代码体现 // 设置 editing 属性 tableView?.editing = true // 这个设置的时候是有动画效果的 tableVi ...
- IOS第七天(6:UiTableView编辑模式, 拖动位置 ,滑动删除)
**********UiTableView编辑模式, 拖动位置 ,滑动删除 #import "HMViewController.h" @interface HMViewContro ...
- UITableView 编辑模式(增加-删除-移动---自定义左滑 title)
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...
- UITableView编辑模式
UITableView有两种模式,普通模式和编辑模式.在编辑模式下可以对cell进行排序.删除.插入等等. 如何进入编辑模式 调用tableView的setEditing(editing: Bool, ...
- UITableView 编辑模式(增加-删除-移动---自定义左滑 title) xib cell
参考: http://www.open-open.com/lib/view/open1430008922468.html - (void)viewDidLoad { [super viewDidLo ...
- iOS开发——UI进阶篇(四)tableView的全局刷新,局部刷新,左滑操作,左滑出现更多按钮,进入编辑模式,批量删除,自定义批量删除
首先创建项目,在storyboard如下布局控件,设置好约束 然后创建cell模型类XMGWineCell数据模型类XMGWine创建UITableView,设置数据源协议,实现数据源方法懒加载数据这 ...
- IOS第13天(3,私人通讯录,登陆状态数据存储,数据缓存, cell的滑动删除,进入编辑模式,单个位置刷新 )
*****联系人的界面的优化 HMContactsTableViewController.m #import "HMContactsTableViewController.h" # ...
随机推荐
- .net抓取网页信息 - Jumony框架使用1
往往在实际开发中,经常会用到一些如抓取网站信息之类的的操作,往往大家采用的是用一些正则的方式获取,但是有时候正则是很死板的,我们常常试想能不能使用jquery的选择器,获取符合自己要求的元素,然后进行 ...
- Asp.net MVC @Html.DisplayNameFor中文乱码解决办法
这是utf8的问题,vs2015默设要用utf8编码,以前的gb2312皆会产生乱码 这问题还是会继续发生,因为常会复制旧程序档进新方案.它会出现很奇怪的错误,而不是这么明显的乱码,你怎么抓bug都抓 ...
- Android无法生成R文件的终极解决办法
R文件如果在clean项目(Project—>Clean)和 Fix Project Properties(如下图): 如果在第一步无法解决的的时候,那可能原因就是资源文件调用的错误,比如资 ...
- SOA面向服务化编程架构(dubbo)
dubbo 是阿里系的技术.并非淘宝系的技术啦,淘宝系的分布式服务治理框架式HSF啦 ,只闻其声,不能见其物.而dubbo是阿里开源的一个SOA服务治理解决方案,dubbo本身 集成了监控中心,注 ...
- 也来一篇关于Infragistics WPF Report的使用教程 (一)
前言 Infragistics Report是一款比較灵活的报表控件, 比微软的rdlc控件至少在页面打印上, 页面的控制比較好调整. 这里使用的是Infragistics Ultimate v14 ...
- 利用text插件和css插件优化web应用
JavaScript的模块化开发到如今,已经相当成熟了,当然,一个应用包含的不仅仅有js,还有html模板和css文件. 那么,如何将html和css也一起打包,来减少没必要的HTTP请求数呢? 本文 ...
- AS3 Signals
在项目中,使用as3内置事件框架必须通过自定义事件才可以实现值的传递,大量自定义事件.定义常量和整个事件派发的管理.添加侦听器.移除侦听器,或多或少都会带来大量的代码,而signals这个框架思想原来 ...
- du 和 df命令的区别(超赞)
du和df命令都被用于获得文件系统大小的信息:df用于报告文件系统的总块数及剩余块数,du -s /<filesystem>用于报告文件系统使用的块数.但是,我们可以发现从df命令算出的文 ...
- php生成excel或php生成csv
一.php生成excel 使用phpexcel类文件生成 二.php生成csv <?php$action ="make";if ($action=='make'){ $fp ...
- cooking java ——加密解密
java安全与密码概述 主要分为三部分: 密码学基础,包括:相关术语:分类:常用安全体系. java的安全组成:jdk以及第三方扩展. 相关实现代码,包括:base64.MD5········ 密码学 ...