UITableView的编辑模式

UITableView可以分普通模式和Editing模式两种,这里我们着重讨论Editing模式,Editing模式中又分三种操作:Insert、Delete、 Reallocted。Insert和Delete针对数据源内容的修改,而Reallocated是针对数据源位置的修改。下面分别讨论。

一、Insert Or Delete

当UITableView接收到 setEditing:animated:时,它会将同样的消息转发给每一个可见行,大致会经历如下步骤,引用至官方:

UITableView的编辑模式

  1. 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.

  2. 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.

  3. 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.

  4. 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:

总结起来:

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模式。这里引用官方的图片以及步骤:

UITableView的编辑模式

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:

  1. 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.

  2. 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.

  3. 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.

  4. 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的位置。

  1. - (NSIndexPath *)tableView:(UITableView *)tableView
  2.        targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
  3.        toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
  4.    NSDictionary *section = [data objectAtIndex:sourceIndexPath.section];
  5.    NSUInteger sectionCount = [[section valueForKey:@"content"] count];
  6.    if (sourceIndexPath.section != proposedDestinationIndexPath.section) {
  7.        NSUInteger rowInSourceSection =
  8.             (sourceIndexPath.section > proposedDestinationIndexPath.section) ?
  9.               0 : sectionCount - 1;
  10.        return [NSIndexPath indexPathForRow:rowInSourceSection inSection:sourceIndexPath.section];
  11.    } else if (proposedDestinationIndexPath.row >= sectionCount) {
  12.        return [NSIndexPath indexPathForRow:sectionCount - 1 inSection:sourceIndexPath.section];
  13.    }
  14.    // Allow the proposed destination.
  15.    return proposedDestinationIndexPath;
  16. }

代码1.1

上一篇:IOS第七天(6:UiTableView编辑模式, 拖动位置 ,滑动删除)


下一篇:linux ubuntu平台下安装Scrapy