使用 SLExpandableTableView 移除 UITableViewRow

Remove UITableViewRow With SLExpandableTableView

我目前有一个 slexpandabletable视图(部分可以展开以显示行)

table 使用 fetchedResultsController 填充。

table 正常工作。

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

下面的代码是尝试删除行

- (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',正在终止应用程序,原因:'attempt to delete row 93 from section 0 which only contains 14 rows before the update'

我尝试了以下

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

但同样的问题又出现了,但行不同

由于未捕获的异常 'NSInternalInconsistencyException',正在终止应用程序,原因:'attempt to delete row 77 from section 0 which only contains 14 rows before the update'

我认为问题与 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;
        }
    }
}

抱歉,如果我粘贴了太多代码,但我想让您更好地理解发生了什么。

如何删除 uitable视图行?

这看起来不错(通常怀疑),这意味着您(或此第三方框架)正在向 table 视图提供意外信息。特别是偏离了那么大的数字(14 个中的 77 个)可能意味着第三方代码正在做某事 wrong/improper.

您正在正确地向 table 视图报告(尽管我个人不会将开始...结束...放在那里)。假设您没有实施 -numberOfRowsInTableView: 那么可能是在第三方代码中。如果您正在实施 -numberOfRowsInTableView: 然后显示该代码,可能其中有些东西不可靠。