【发布时间】:2015-06-24 09:03:04
【问题描述】:
我对 IOS 编程很陌生,这将是我的第一个应用程序。我有一个 TableViewController 来显示数据历史记录(CoreData),我正在尝试实现一个 SearchBar(iOS8)。除了布局的一个小问题外,它工作正常(请参阅屏幕截图,tableView 没有全宽,并且有这个 lupe...?,有人知道这是为什么吗?)
我更大的问题是,当我在不使用搜索栏的情况下删除一行时,它可以工作,但是当我搜索一个条目并尝试删除时,它删除了它,但我得到了一个异常,并且视图卡在了红色“删除”-按钮。如果我返回主视图并返回 tableview,则视图会更新..
*** -[UITableView _endCellAnimationsWithContext:] 中的断言失败,/SourceCache/UIKit_Sim/UIKit-3318.93/UITableView.m:1582 2015-06-24 10:55:25.635 踏板 [9203:284856] CoreData:错误:严重的应用程序错误。在调用 -controllerDidChangeContent: 期间,从 NSFetchedResultsController 的委托中捕获了异常。无效更新:第 0 节中的行数无效。更新后现有节中包含的行数 (1) 必须等于更新前该节中包含的行数 (1),加上或减去数字从该部分插入或删除的行数(0 插入,1 删除)加上或减去移入或移出该部分的行数(0 移入,0 移出)。与 userInfo (null)
这是我认为相关的两种方法的代码(如果我错了,请纠正我)
- (void)tableView:(UITableView *)inTableView commitEditingStyle:(UITableViewCellEditingStyle)inStyle forRowAtIndexPath:(NSIndexPath *)inIndexPath {
if(inStyle == UITableViewCellEditingStyleDelete) {
//[inTableView beginUpdates];
RecordingInfo *theItem = [self entryAtIndexPath:inIndexPath];
NSError *theError = nil;
[self.managedObjectContext deleteObject:theItem];
if([self.managedObjectContext save:&theError]) {
if(self.searchController.active) {
NSMutableArray *theResult = [self.searchResult mutableCopy];
[theResult removeObjectAtIndex:inIndexPath.row];
self.searchResult = theResult;
[inTableView deleteRowsAtIndexPaths:@[inIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
NSLog(@"deletion successful");
}
}
else {
NSLog(@"Unresolved error while deleting %@", theError);
}
//[self.tableView endUpdates];
}
}
我是否可能需要在控制器中添加类似的 if-question:didChangeObject:?
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationRight];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray
arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray
arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
如果有人能帮助我,我会很高兴,我现在正试图永远解决这个问题! 谢谢!
【问题讨论】:
-
你有没有:func controllerWillChangeContent(controller: NSFetchedResultsController) { self.tableView.beginUpdates() }
-
@Mikael 是的,我有,而且我也有:controllerDidChangeContent:(NSFetchedResultsController *)controller { [self.tableView endUpdates]; }
-
还要检查你的:func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { let sectionInfo = self.fetchedResultsController.sections![section] as! NSFetchedResultsSectionInfo 返回 sectionInfo.numberOfObjects }
-
我觉得应该也是对的: - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // 返回section中的行数。 if (self.searchController.active) { return [self.searchResult count]; } else { id
sectionInfo = [[self.fetchedResultsController section] objectAtIndex:section];返回 [sectionInfo numberOfObjects]; } } -
好吧,如果我是你,我会在这里设置一个断点。这个“if (self.searchController.active)”很可疑,似乎与您的问题有关。
标签: ios objective-c uitableview core-data