【问题标题】:iOS/Objective-C: Swipe to Left causing crash in tableview created with codeiOS/Objective-C:向左滑动导致使用代码创建的 tableview 崩溃
【发布时间】:2019-01-07 04:26:29
【问题描述】:

当您向左滑动删除时,我有一个 tableview 崩溃。当 tableview 在 cellfor row atindexpath 引用 indexpath 的某个点重新加载时,它在删除后崩溃。另一个问题是,在服务器上收到成功删除的通知之前,该表不会重新加载。尽管如此,我无法弄清楚导致崩溃的原因。是否需要其他代表。我必须手动更改索引路径吗?提前感谢您的任何想法或建议。

这是我的代码:

    - (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;
}

【问题讨论】:

  • 崩溃说明了什么?你的堆栈跟踪?
  • 这里有一些问题。您没有显示_managedObjectContext 的来源。是[self getItems]吗?在postToServerDelete 中,您有andItem:(Steps*)stepToDelete... 似乎您复制/粘贴了一些代码并且没有更改“模板”以适应您正在做的事情。 deleteObject 是否将它从我想象[self getItems] 返回的数组中删除?如果没有,您需要这样做,否则在重新加载表格时您将到达一个悬空指针。
  • 崩溃是 lldb。如果您认为它会有所帮助,可以发布 bt...太长,无法放入评论
  • managedObjectContext 来自核心数据,该行成功删除了对象。 [self getItems 返回从 Core Data 获取的这些自定义对象的数组,我还添加了一行以确保它被刷新——见上文。我还尝试手动从数组中删除对象,但没有区别。)问题似乎是即使在删除之后,当表加载时,它也会尝试加载旧的项目数,而不是数字(一个less) 删除后和最后一个循环,它崩溃了。

标签: ios objective-c tableview delete-row


【解决方案1】:

这条崩溃线

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

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

  1. self getItems 返回的不是数组(或 null)。
  2. 如果它确实返回一个数组,那么您指定的索引不再存在。

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

【讨论】:

  • 问题似乎是2。数组不是零。删除后,仍调用 cellForRow 相同的次数。所以如果有 4 个项目,而你删除了一个,它仍然会被调用 4 次。第四次,该索引处的 Array 中不再有对象。是否需要包含其他方法以便 cellforrow 知道已发生删除?
  • 如果你对 cellForRow 和 commitEditing 委托使用相同的对象,那么应该没有问题。尝试替换: Items* item = [[self getItems] objectAtIndex:indexPath.row]; with Items* item = [_myItems objectAtIndex:indexPath.row];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-13
  • 1970-01-01
  • 2012-05-26
  • 1970-01-01
相关资源
最近更新 更多