由于 NSManagedObjectContext 保存而更新 NSFetchedResultsController 时延迟 UITableView 更新
我通过使用带有 NSFetchedResultsController 的谓词对 UITableView 进行排序。但是,当我在详细视图中进行更改并保存它时,我更改的对象会立即放置在 UITableView 中的新位置。
我有类似于邮件中的消息视图的向上/向下按钮。这种行为会破坏项目的排序,我想推迟此更改,直到用户出于 UX 原因退出 UITableView。我在 UITableViewController 中实现了 NSFetchedResultsControllerDelegate 方法。有没有一种简单/聪明的方法可以做到这一点?
编辑:下面是 UITableViewController 中实现的 NSFetchedResultsControllerDelegate 方法。
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (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;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}
I am sorting the UITableView by using a predicate with NSFetchedResultsController. But, when I make a change in a detail view and save it, the object I changed immediately gets placed in its new location in the UITableView.
I have up/down buttons similar to the message view in Mail. This behavior disrupts the ordering of items, and I'd like to delay this change until the user backs out to the UITableView for UX reasons. I implemented the NSFetchedResultsControllerDelegate methods in the UITableViewController. Is there an easy/clever way to do this?
Edit: Below are the NSFetchedResultsControllerDelegate methods implemented in the UITableViewController.
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (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;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题不在于 NSFetchedResultsControllerDelegate 方法。仅当表可见时数据模型发生更改时,这些方法才会发挥作用。
您的问题是您在表视图关闭屏幕且处于非活动状态时更改数据。表视图旨在自动显示给定的数据模型。当您从详细视图返回表视图时,它会自动重新加载其数据,其中现在包含在详细视图中所做的更改。无论您如何向表提供数据,它都会这样做。
阻止表在您需要之前显示更改的唯一方法是,在您希望显示更改之前不要将数据添加到数据模型。您必须将数据从详细视图传递回非托管对象中的表,并在您希望它出现时在表视图控制器中创建托管对象。
然而,从UI设计的角度来看,我建议你重新考虑你的设计。如果您在重新加载表之前不更改表,那么您将如何向用户发出您将进行更改的信号? UI 会无缘无故突然更新吗?用户必须启动它吗?如果他们忘记了怎么办?
我认为用户会期望详细视图中所做的任何更改都能立即反映在表视图中,因为这实际上是所有 tableview-detailView 配对的工作方式。例如,如果您更改了地址簿的联系人详细信息中的联系人姓名,则当您返回联系人列表时,该姓名会立即反映出来。
The problem is not in the
NSFetchedResultsControllerDelegate
methods. Those methods only come into play only if something changes the data model while the table is visible.Your problem is that you change the data while the table view is off screen and inactive. Tableviews are designed to automatically display the data model given to them. When you return to the tableview from the detail view, it automatically reloads its data which now contains the changes made in the detail view. It would do that regardless of how you provided data to the table.
The only way to prevent the table from displaying the changes until you want it to is to not add the data to the data model until you want the change to appear. You would have to pass the data from the detail view back to the table in a non-managedObject and create the managedObject in the tableview controller when you wanted it to appear.
However, from a UI design perspective, I suggest you rethink your design. If you don't change the table until after it reloads, how will you signal the users that you are going to make the change? Will the UI just suddenly update for no apparent reason? Will the user have to initiate it? What if they forget?
I think that users will expect any changes made in the detail view to be instantly reflected in the tableview because that is how virtually all tableview-detailView pairings work. For example, if you change a contact's name in the contact detail of the AddressBook that is reflected instantly when you go back to list of contacts.
我使用的一个技巧是当表视图控制器消失时调用 beginUpdates,然后在它出现时调用 endUpdates:
这样,获取控制器可以继续工作,并且您可以继续将更新用于其他事情。
One trick I've used is to call beginUpdates when the table view controller disappears, and then endUpdates when it will appear:
This way the fetch controller can keep working and you can keep using the updates for other things.