使用SLExpandableTableView删除UITableViewRow

时间:2015-10-21 13:48:38

标签: uitableview core-data nsfetchedresultscontroller

我目前有一个slexpandabletableview(章节可以展开以显示行)

使用fetchedResultsController填充表格。

该表正在按预期工作。

然而,当我尝试删除某个部分中的行时,我得到一个奇怪的回应。

以下代码是尝试删除行

- (void)didClickOnCellAtIndex:(NSInteger)cellIndex withData:(id)data
{
  // Delete the object
  NSIndexPath *path = [NSIndexPath indexPathForItem:cellIndex inSection:0];

NSManagedObject *punToDelete = [self.fetchedResultsController objectAtIndexPath:path];
[appDelegate.managedObjectContext deleteObject:punToDelete];
[appDelegate.managedObjectContext save:nil];}

错误信息如下

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:'尝试从更新前仅包含14行的第0部分删除第93行

我尝试了以下

    NSIndexPath *path = [NSIndexPath indexPathForItem:0 inSection:0];

但同样的问题又回来了,但是有一个不同的行

由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'尝试从更新前仅包含14行的第0部分删除第77行

我认为问题与SLExpandableTableView有关吗?

如果它有助于我的fetchresultscontroller方法看起来像这样

    -(void)fetchResultsbyEntity :(NSString*)entity sortWithKey:(NSString*)key{

 // Initialize Fetch Request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:entity];

// Add Sort Descriptors
[fetchRequest setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:key ascending:YES]]];

// Initialize Fetched Results Controller
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:appDelegate.managedObjectContext sectionNameKeyPath:nil cacheName:nil];

// Configure Fetched Results Controller
[self.fetchedResultsController setDelegate:self];



// Perform Fetch
NSError *error = nil;
[self.fetchedResultsController performFetch:&error];

if (error) {
    NSLog(@"Unable to perform fetch.");
    NSLog(@"%@, %@", error, error.localizedDescription);
}
NSLog(@"fetched results contain %i",self.fetchedResultsController.fetchedObjects.count);
}

我在滑动删除方法中遇到完全相同的问题。

我在NSFetchedResults控制器的委托方法中有beginUpdates和endUpdates

  - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
    switch (type) {
        case NSFetchedResultsChangeInsert: {
            [self.mainTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        }
        case NSFetchedResultsChangeDelete: {
            [self.mainTableView beginUpdates];

            [self.mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [self.mainTableView endUpdates];

            break;
        }
        case NSFetchedResultsChangeUpdate: {
            //[self configureCell:(TSPToDoCell *)[self.tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            NSLog(@"results changed update called");
            break;
        }
        case NSFetchedResultsChangeMove: {
            [self.mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [self.mainTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        }
    }
}

很抱歉,如果我粘贴了太多代码,但我想让你很好地了解最新情况。

如何删除uitableview行?

1 个答案:

答案 0 :(得分:1)

看起来很好(通常的嫌疑人),这意味着您(或此第三方框架)向表视图提供意外信息。特别是被大量的数字(14个中的77个)关闭可能意味着第三方代码做错了/不正确。

您正在向桌面视图正确报告(虽然我个人不会把开头......结束......)。假设您没有实现-numberOfRowsInTableView:,则可能是在第三方代码中。如果您正在实施-numberOfRowsInTableView:,那么就显示该代码,也许那里有些不可思议的东西。