如何保持UITableView的数据源顺序响应用户移动的行? [英] How to keep UITableView's datasource ordered in response to user moving rows around?

查看:133
本文介绍了如何保持UITableView的数据源顺序响应用户移动的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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. 对象订单1

  2. 订单2对象

  3. 对象订单3

要求我'面向的是用户可以移动行。移动后:

The requirement that I'm facing is that the user can move rows around. After move:


  1. 对象订单2

  2. 对象订单1

  3. 订单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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆