2016-02-12 70 views
0
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 

     PFObject *object = [_tdlArray objectAtIndex:(_tdlArray.count - indexPath.row -1)]; 
     [object deleteInBackground]; 

     //found the code for removing a row. 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 
     [tableView reloadData]; 
     [object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
      if (!succeeded){ 

       [tableView reloadData]; 

      } 

     }]; 

    } 

} 

我能够成功移除数据,但每次点击删除按钮时,我的应用都会崩溃。我认为这事做与[NSArray arrayWithObject:indexPath]轻扫即可删除崩溃时的崩溃

这些错误消息

Assertion failure in -[UITableView _endCellAnimationsWithContext:] 
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' 
+0

是否要更新您的内部状态,以便['的tableView:numberOfRowsInSection:'](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/index.html#//apple_ref/occ/intfm/UITableViewDataSource/tableView:numberOfRowsInSection :)当被问到时会返回正确数量的东西吗? –

+0

您正在删除后台线程中的对象,但立即调用deleteRowsAtIndexPath。一旦对象成功删除,您需要使用回调并调用deleteRowsAtIndexPath。 – beyowulf

+0

@beyowulf对不起,我没有得到你在说什么 –

回答

1

你要删除的对象,然后重新加载数据。不要异步调度要删除的对象,然后告诉tableview你正在删除行,因为对象可能还没有被删除,所以你得到的错误。在删除对象后,使用回调块更新tableview,这样可以确保该对象已被删除。另外,如果您有本地存储的数据未绑定到服务器上的数据,则还需要从中删除该对象。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
     if (editingStyle == UITableViewCellEditingStyleDelete) { 
      //not sure how you're calculating the index here 
      PFObject *object = [_tdlArray objectAtIndex:(_tdlArray.count - indexPath.row -1)]; 
      NSMutableArray *mutArray = [_tdlArray mutableCopy]; 
      [mutArray removeObject:object]; 
      _tdlArray = [NSArray arrayWithArray:mutArray]; 
      [object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
       if (!succeeded){ 
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 
        [tableView reloadData]; 

       } 

      }]; 

     } 

    } 
+0

其实,我没有收到错误,但它并没有删除该行。 –

+1

我更新了我的答案。你可以把一些断点或NSLog的一些事情,看看是否完成块被称为? – beyowulf

+0

你制定的新代码太棒了!我有点觉得它与NSArray的事情有关,我只是不知道如何写它或逻辑如何工作。但是,谢谢! –