在我的应用程序中,我有一个 View Controller ,它是 NSFetchedResultsController 的委托(delegate)以及 UITableView 的委托(delegate)和数据源。
当按下导航 Controller 中的添加按钮时,下一个 View 被正确推送,我可以正确添加一个新的 Person 实体。
我的问题是,当按下添加按钮时,随着新 View 的推送,一个空白行被添加到 tableView,并且在正确创建新实体记录后仍然存在
这是添加按钮的目标操作:
- (void)addPerson:(id)sender
{
AddPersonViewController *addPersonController = [[AddPersonViewController alloc] initWithNibName:@"AddPersonViewController" bundle:nil];
[addPersonController setPerson:[NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext]];
[self.navigationController pushViewController:addPersonController animated:YES];
}
创建空白行的代码(来自 NSFetchedResultsControllerDelegate 的苹果文档)在这里:
- (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:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
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;
}
}
关于如何停止创建此空白行的任何想法?
最佳答案
打电话
[NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext]
在 addPerson
中创建一个新的 Person 对象并将其添加到托管对象上下文中。如果 TableView 的获取结果 Controller 被配置为获取所有 Person 对象,这将导致这个新对象的新(空白)表行。
我不知道您的 AddPersonViewController
是如何工作的。如果它修改了通过 setPerson
给定的对象,那么应该更新表格行。如果它创建了一个新的 Person 对象,那么第一个(空白)条目将保留。
您可能应该延迟创建新的 Person 对象,直到 AddPersonViewController
具有实际创建和填充对象的所有数据。您可以通过将 insertNewObjectForEntityForName:
调用移动到 AddPersonViewController
,或者在您的 TableView Controller 中使用从 AddPersonViewController< 调用的委托(delegate)方法来执行此操作
.
关于iphone - NSFetchedResultsController 添加实体在 tableView 中插入空白行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12872090/