Iphone 在UITableView中删除行时出错(仅在iOS7中)

Iphone 在UITableView中删除行时出错(仅在iOS7中),iphone,uitableview,ios7,cell,Iphone,Uitableview,Ios7,Cell,我的应用程序在iOS6中运行得很酷,但当我使用与我使用的代码相同的代码更新iOS7时,当我尝试删除表中的一行时,会出现以下错误: 2013-10-02 17:44:11.344 Goal[1877:a0b] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2903.2/UITableView.m:1330 2013-10-02 17:

我的应用程序在iOS6中运行得很酷,但当我使用与我使用的代码相同的代码更新iOS7时,当我尝试删除表中的一行时,会出现以下错误:

2013-10-02 17:44:11.344 Goal[1877:a0b] *** Assertion failure in -[UITableView
     _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2903.2/UITableView.m:1330
2013-10-02 17:44:11.384 Goal[1877:a0b] *** 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 (1) must be equal to    the number of rows contained in that section 
before the update (1), 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).'
如果你需要,这里有一些方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        // deleting
        NSManagedObjectContext *moc = self.managedObjectContext;
        Goal *goalToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
        [moc deleteObject:goalToDelete];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }

}
另一个

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"GoalCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

// Configure the cell...
cell.textLabel.backgroundColor = [UIColor clearColor];

Goal *goal = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = goal.title;

return cell;
}

不,我没有忘记检查我的手机识别码,

错误信息非常清楚:

  • 更新前的行数=1
  • 删除的行数=1
  • 更新后的行数=1(应为0)
numberOfRowsAtIndexPath方法实现错误。它返回的行数应比删除前少1行。此方法的返回值应与UITableView上预期的行数匹配


此外,在调用[tableView deleteRowsAtIndexPaths:@[indexPath]with RowAnimation:UITableViewRowAnimationFade]之前,您的代码将在任何iOS版本上崩溃,而不仅仅是在7版本上;
减少numberOfRowsInSection函数返回的“numberOfRows”。

我猜您会遇到一个错误,因为您在更新MOC后调用了
DeleteRowsAndExpaths
两次:一次在
CommittedItingStyle
中,另一次在
NSFetchedResultsControllerDelegate
方法中

但真正的问题是根本不应该调用
deleteRowsAtIndexPaths

当用户操作删除行时,表视图通过
committeeditingstyle
delegate方法通知您。然后,您的工作是更新数据模型,使其与表视图已经知道的内容保持一致。你不需要通知桌子


只有在以编程方式修改数据模型时,才需要通过调用insert/delete/move方法通知表视图。

您关于在
CommittedItingStyle
中未调用
deleteRowsAtIndexPath
的评论不正确:“数据源通过调用UITableView方法insertRowsAtIndexPaths:withRowAnimation:或deleteRowsAtIndexPaths:withRowAnimation:(视情况而定)提交插入或删除。”@Rizwan你完全正确。谢谢你指出这一点。我回头看了看自己的代码,实际上,
deleteRowsAtIndexPaths
被另一个方法间接调用。我觉得有点奇怪,
committeeditingstyle
moverowatinexpath
在这方面的工作方式不同。如果您有一个更高级的UITableViewSource实现,其中有一个变量、属性或方法跟踪行数(如果使用节,则在节中),Mehmet是对的,您必须确保减少行数。在调用deleteRowsAtIndexPath之前,行数必须减少已删除的行数。谢谢你,梅米特!