如何在目标c中删除表格视图中的多个部分?

Posted

技术标签:

【中文标题】如何在目标c中删除表格视图中的多个部分?【英文标题】:How to delete multiple sections in table view in objective c? 【发布时间】:2016-10-30 16:26:33 【问题描述】:

当我尝试删除多个部分时出现此错误。

由于未捕获的异常而终止应用程序

'NSInternalInconsistencyException',原因:'试图删除部分 5,但更新前只有4节'

这是我的代码:

NSArray *selectedRows = [self.tableView indexPathsForSelectedRows];
    
int i=0;

for (NSIndexPath *selectionIndex in selectedRows)
    
    
    NSManagedObjectContext *context = [self managedObjectContext];
    
    NSManagedObject *managedObject = [arrayToDelete objectAtIndex:i];
    [self.devices removeObject:[arrayToDelete objectAtIndex:i++]];
    [context deleteObject:managedObject];
    [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:selectionIndex.section] withRowAnimation:UITableViewRowAnimationFade];



[self saveContext];

[arrayToDelete removeAllObjects];

【问题讨论】:

【参考方案1】:

当您尝试删除数组中的某些内容时,我建议您反转方向。 那就是:

//int i=0;

for (int i = [selectedRows count] - 1; i = 0; i-- )

NSIndexPath *selectionIndex = selectedRows[i];
    NSManagedObjectContext *context = [self managedObjectContext];

    NSManagedObject *managedObject = [arrayToDelete objectAtIndex:i];
    [self.devices removeObject:[arrayToDelete objectAtIndex:i]];
    [context deleteObject:managedObject];
    [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:selectionIndex.section] withRowAnimation:UITableViewRowAnimationFade];

【讨论】:

以上是关于如何在目标c中删除表格视图中的多个部分?的主要内容,如果未能解决你的问题,请参考以下文章