繁体   English   中英

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

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

当您向左滑动以删除时,我的表视图崩溃了。 当表视图在引用索引路径的atforpath行的cellfor行中的某个点重新加载时,删除后崩溃。 另一个问题是,直到在服务器上收到删除成功的通知后,表才重新加载。 但是,我不知道是什么原因导致了崩溃。 是否需要其他代表。 我是否必须手动更改索引路径? 预先感谢您的任何想法或建议。

这是我的代码:

    - (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返回的不是数组(或null)。
  2. 如果确实返回数组,则您指定的索引不再存在。

我怀疑是情况2,因为您提到了删除。 而且我看到了代码,您似乎删除了_myItems,但是您引用了cellForRow中的[self getItems]。 这两个必须是同一对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM