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

时间:2014-01-15 08:11:48

标签: ios objective-c uitableview

我需要在用户点击删除按钮后退出删除模式。我想显示一些活动指示器,并在实际删除单元格之前等待服务器对删除操作的响应(如果服务器没有响应,则等待)。我想从委托方法执行此操作:

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

我该怎么做?

3 个答案:

答案 0 :(得分:4)

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

但是,tableView:commitEditingStyle:forRowAtIndexPath:的实现需要稍微延迟执行。图7-1下方参考Table View Programming Guide for iOS中的注释指出:

  

注意:数据源不应该从tableView的实现中调用setEditing:animated::commitEditingStyle:forRowAtIndexPath:。如果由于某种原因必须使用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];
}

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

答案 1 :(得分:1)

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

[self.tableView reloadData];

我不确定这是否是100%正确的方法,但它适用于我。所以你的功能可能是这样的:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Do your processing.

[self.tableView reloadData];

}

然后应清除红色删除按钮并刷新数据。

您也可以使用通话

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

但根据我的经验,reloadData调用似乎可以解决这个问题

答案 2 :(得分:0)

嗨,让我直截了当地说明:您的数据源实际上是在线的,您希望在更新tableview之前确认它已被删除 - 同时您要显示AI活动指示器。

如果这是您想要的,那么您可以从这个方法开始

  - (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

  }

一旦服务器响应取决于它是否成功,你可以删除记录或重新加载整个数组,如果表很小,这是优选的,以确保数据同步 -

 ....  
    [activityindicator StopAnimating];

   if (success) {

     [self.mutableArray_data_source removeObjectAtIndex:row_index];}
  else {
       NSLog .... 
         }

   [self.tableView reloadData];  // resets the delete button.