iOS/Objective-C:向左滑动导致使用代码创建的表视图崩溃

iOS/Objective-C: Swipe to Left causing crash in tableview created with code

我有一个 table 视图在您向左滑动删除时崩溃。当 table 视图在引用索引路径的索引路径的行的单元格中的某个点重新加载时,它在删除后崩溃。另一个问题是 table 在收到服务器上成功删除的通知之前不会重新加载。尽管如此,我无法弄清楚是什么导致了崩溃。是否需要另一个代表。我必须手动更改索引路径吗?提前感谢您的任何想法或建议。

这是我的代码:

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (tableView == _myTableView ) {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
           Items *itemToDelete = [_myItems objectAtIndex:indexPath.row];     
            NSNumber *iidToDelete = itemToDelete.iid;     
     //CREATE DICTIONARY AND SEND TO SERVER FOR DELETION           
                    [self postToServerDelete:data andItem:itemToDelete];
        }
        }
    }
    -(void) postToServerDelete: (NSData *) data andItem:(Items *)itemToDelete  {
          //SEND TO SERVER
                        if (successint==1) {
                            dispatch_async(dispatch_get_main_queue(), ^{              
                                [_managedObjectContext deleteObject:itemToDelete];
                                NSError* error;
                       [self getItems];//refreshes items list from managedobjectcontext
                            if ([_managedObjectContext save:&error]) {   
                                [_myTableView reloadData];
                            }        
                        });
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = nil;
        static NSString *StepRowIdentifier = @"RowIdentifier";
        cell = [tableView dequeueReusableCellWithIdentifier:RowIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]
                    initWithStyle:UITableViewCellStyleDefault reuseIdentifier:RowIdentifier] ;   
        }
        //LINE WHERE CRASH OCCURS
        Items* item = [[self getItems] objectAtIndex:indexPath.row];
        NSString *steptext= item.name;
        cell.textLabel.text=name;
    return cell;
}

这条速成线

 Items* item = [[self getItems] objectAtIndex:indexPath.row];

基本上可能是由两件事引起的:

  1. self getItems is returning other than array (or null).
  2. if it does return an array, then the index at which you specified no longer exist.

我怀疑是情况2,因为你提到删除。我看到代码,你似乎删除了_myItems,但你在cellForRow中引用了[self getItems]。这两个需要是同一个对象。