如何保持UITableView的数据源顺序响应用户移动的行? [英] How to keep UITableView's datasource ordered in response to user moving rows around?
问题描述
我有一个UITableView,其中一个部分由NSFetchedResultsController管理。它获取核心数据对象并按order属性对其进行排序:
I have a UITableView and one of its sections is managed by a NSFetchedResultsController. It fetches core data objects and keep them sorted by the "order" attribute:
- 对象订单1
- 订单2对象
- 对象订单3
要求我'面向的是用户可以移动行。移动后:
The requirement that I'm facing is that the user can move rows around. After move:
- 对象订单2
- 对象订单1
- 订单3对象
我需要做些什么来确保核心数据中的托管对象反映用户移动表视图行所导致的顺序变化?当我使用GMGridView时,会有一个交换两个对象的回调。在这种情况下,改变他们的顺序工作,但我没有看到这样的功能为UITableView。
What do I need to do to ensure that the managed objects in core data reflect changes in order resulting from user moving table view rows around? When I'm working with GMGridView, there's a callback that exchanges two objects. In such case, changing their order works, but I do not see such function for a UITableView.
我是否需要手动更改移动行下方所有对象的order属性?
Would I need to manually change the order attribute for all objects that exist below the moved row?
推荐答案
你可以使用这个:
- (NSIndexPath *) tableView:(UITableView *)tableView
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
通过交换对象订单,重新订购数据源。
Plus re-ordering your data-source, by exchanging the objects order.
这篇关于如何保持UITableView的数据源顺序响应用户移动的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!