当用户使用滑动手势对其进行编辑时,如何离开UITableViewCellEditingStyleDelete模式

用户点击删除button后,我需要离开删除模式。 我想显示一些活动指标,并在实际删除单元格之前(或者如果服务器没有响应),等待删除操作的服务器响应。 这个动作我想从委托方法执行:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 

我该怎么做?

要隐藏“删除”button,您需要使用setEditing:animated:方法。

然而,你的tableView:commitEditingStyle:forRowAtIndexPath:执行需要稍微延迟执行。 图7-1中iOS参考表视图编程指南中的注释表明:

注意:数据源不应该在其实现的tableView:commitEditingStyle:forRowAtIndexPath:中调用setEditing:animated:。 如果由于某种原因它必须在延迟之后使用performSelector:withObject:afterDelay:方法来调用它。

所以你的实现可能是这样的:

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // remove delete button only after short delay [self performSelector:@selector(hideDeleteButton:) withObject:nil afterDelay:0.1]; } } - (void)hideDeleteButton:(id)obj { [self.tableView setEditing:NO animated:YES]; } 

上面的代码使用户按下后,删除button在0.1秒后滑开。 那么你当然需要在等待完成动作的时候阻止它回到编辑模式。 为此,您可以重写UITableViewDataSource方法tableView:canEditRowAtIndexPath:以防止在等待时再次编辑同一单元格或整个表格。

在我的代码中,我打电话给

[self.tableView reloadData];

我不确定这是否是100%正确的方法,但对我有用。 所以你的function可能是这样的:

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { // Do your processing. [self.tableView reloadData]; } 

然后应该清除红色的删除button并刷新您的数据。

你也可以随时打电话

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

但根据我的经验,它的reloadData调用似乎有伎俩

嗨,让我直接得知:你的数据源实际上是在线的,你需要确认它在你更新你的tableview之前被删除 – 同时你想显示一个AI Activity指示器。

如果这是你想要做的,那么你从这个方法开始

  - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { // Get the key of the data you want to delete. data_struct_instance * data = [self.mutableArray_data_source objectForRowAtIndexPath:indexPath]; NSString *data_key = data.key; row_index = indexPath.row; // set a property to preserve the row (or index path for multilevel data) for use when you delete the the record later. [self start_UIActivityIndicator]; [self callonline_with_delete_command:data_key]; // send the request to delete the record } 

一旦服务器响应是否成功,您可以删除logging或重新加载整个数组,如果表很小,则最好确保数据同步 –

  .... [activityindicator StopAnimating]; if (success) { [self.mutableArray_data_source removeObjectAtIndex:row_index];} else { NSLog .... } [self.tableView reloadData]; // resets the delete button.