2016-02-12 71 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的事情有關,我只是不知道如何寫它或邏輯如何工作。但是,謝謝! –