删除 UITableViewCell 后应用冻结

App freezes after deleting UITableViewCell

当我尝试在请求的成功块中删除 UITableViewCell 时出现问题。

在我的

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

我正在向 AFNetworking 提出请求

- (NSURLSessionDataTask *)POST:(NSString *)URLString
                    parameters:(id)parameters
                       success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                       failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure

之后,在成功块中,我通过关闭 SVProgressHUD 和删除单元格来更新 UI:

NSUserDefaults *usd = [NSUserDefaults standardUserDefaults];
[usd removeObjectForKey:@"payViaCreditCard"];
selectedCardID = nil;

[[CreditCardManager sharedCreditCardManager] removeCreditCard:creditCardToDelete];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView reloadData];

最后我的 UI 没有更新并且应用程序冻结,当我重新启动应用程序时,NSUserDefaults 中的数据发生了变化。

如果你做deleteRowsAtIndexPaths:withRowAnimationtableView reloadData就好了。

    [[CreditCardManager sharedCreditCardManager] removeCreditCard:creditCardToDelete];
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    OR 

    [[CreditCardManager sharedCreditCardManager] removeCreditCard:creditCardToDelete];
    [self.tableView reloadData];

要删除单元格,您应该使用

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
   return UITableViewCellEditingStyleDelete; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{
   if (editingStyle == UITableViewCellEditingStyleDelete) 
   {
      NSUserDefaults *usd = [NSUserDefaults standardUserDefaults];
      [usd removeObjectForKey:@"payViaCreditCard"];
      selectedCardID = nil;

      [[CreditCardManager sharedCreditCardManager] removeCreditCard:creditCardToDelete];
      [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
      //[self.tableView reloadData];
    }
}