如果我只是使用tableView:moveRowAtIndexPath:toIndexPath:方法,它的工作原理如上所述,并且该表在点击Done时正确显示了重新排序的行.但是当它返回到表时它不记得新的顺序,所以我在核心数据中添加了一个新的“位置”变量(Int),并且我已经根据它对表进行排序.
我的问题是,在用户移动一行并单击“完成”后,该表会立即将自身重新排序回原来的状态.因此,在我的代码可以正确捕获新订单并将其重新写入核心数据之前,它使用核心数据中的旧(原始)位置变量.
有没有办法在表重新加载之前点击Done时捕获这个新订单?
这是我处理移动的代码:
func tableView(tableView: UITableView!,moveRowAtIndexPath sourceIndexPath: NSIndexPath!,toIndexPath destinationIndexPath: NSIndexPath!) { }
这是我按下Done时调用的代码.我遍历表格的每一行,并根据它的新行在核心数据中重新分配人员的位置变量:
for currentIteration in 0..<tableView.numberOfRowsInSection(0) { var indexPathForCurrentIteration = NSIndexPath(forRow: currentIteration,inSection: 0) let personAtCurrentIndexIteration = fetchedResultsController.objectAtIndexPath(indexPathForCurrentIteration) as PersonModel personAtCurrentIndexIteration.position = indexPathForCurrentIteration.row println("\(personAtCurrentIndexIteration.name) has a position value of \(personAtCurrentIndexIteration.position)") (UIApplication.sharedApplication().delegate as AppDelegate).saveContext() tableView.reloadData(); }
按下完成按钮时调用上面的内容,但我也尝试在moveRowAtIndexPath方法中调用它而没有成功(相同的结果 – 当按下Done时它重新加载表的旧顺序).我也试过注释掉reloadData而没有成功.
我认为我的问题可能在于以上事实并未捕获新的行顺序,而是仍然获得旧的行顺序.如果是这种情况,那么我不知道让它“等待”获得新行顺序的最简单方法.
我开始考虑的一个更长的解决方法是捕获移动的行的初始和最终行值,并根据该更新相应地保留剩余行的值.一些代码在下面,但我停止了,因为我认为可能有一个更聪明的方式.
if((sourceIndexPath.row > indexPathForCurrentIteration.row) && (destinationIndexPath.row > indexPathForCurrentIteration.row)) { //if the moved row was ahead of this row before AND after the move,do nothing (don't change this row's position value) //println("\(personAtCurrentIndexIteration.name)'s position was unchnaged") } else if((sourceIndexPath.row > indexPathForCurrentIteration.row) && (destinationIndexPath.row < indexPathForCurrentIteration.row)) { //if the moved row was ahead of this row before BUT behind it after the move,then this row's position value needs to be bumped up one number personAtCurrentIndexIteration.position = indexPathForCurrentIteration.row+1 //Etc...
提前感谢您提供的任何帮助.
解决方法
>向CoreData添加一些数据时,还要添加orderPosition值
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate let contxt: NSManagedobjectContext = appDel.managedobjectContext! let en = NSEntityDescription.entityForName("MyCoreData",inManagedobjectContext: contxt) let freq = NSFetchRequest(entityName: "MyCoreData") var MyData = contxt.executeFetchRequest(freq,error: nil)! var newItem = Model(entity: en!,insertIntoManagedobjectContext: contxt) var newOrderPosition = MyData.count //here you add some data to your CoreData //and also you must add new order position newItem.orderPosition = orderPosition
>在TableView中,您必须创建一个函数,对将在TableView中显示的数组进行排序.像这样的东西:
func RefreshCoredata() { let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate let freq = NSFetchRequest(entityName: "MyCoreData") let contxt: NSManagedobjectContext = appDel.managedobjectContext! let sortDescriptor = NSSortDescriptor(key: "orderPosition",ascending: true) freq.sortDescriptors = [sortDescriptor] MyData = contxt.executeFetchRequest(freq,error: nil)! }
>请记住,在将数组发送到桌面之前,您必须始终对数组进行排序!
>当您想重新排序表行时必须执行的操作? :
override func tableView(tableView: UITableView,canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool { return true } override func tableView(tableView: UITableView,moveRowAtIndexPath fromIndexPath: NSIndexPath,toIndexPath: NSIndexPath) { let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate let contxt: NSManagedobjectContext = appDel.managedobjectContext! RefreshCoredata() if fromIndexPath.row > toIndexPath.row { for i in toIndexPath.row..<fromIndexPath.row { MyData[i].setValue(i+1,forKey: "orderPosition") } MyData[fromIndexPath.row].setValue(toIndexPath.row,forKey: "orderPosition") } if fromIndexPath.row < toIndexPath.row { for i in fromIndexPath.row + 1...toIndexPath.row { MyData[i].setValue(i-1,forKey: "orderPosition") } contxt.save(nil) RefreshCoredata() }
这需要重置CoreData中的订购位置
>如果要删除某些表行,必须执行哪些操作?
override func tableView(tableView: UITableView,commitEditingStyle editingStyle: UITableViewCellEditingStyle,forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate let contxt: NSManagedobjectContext = appDel.managedobjectContext! RefreshCoredata() contxt.deleteObject(MyData[indexPath.row] as NSManagedobject) for i in indexPath.row + 1..<MyData.count { MyData[i].setValue(i-1,forKey: "orderPosition") } RefreshCoredata() tableView.deleteRowsAtIndexPaths([indexPath],withRowAnimation: .Automatic) contxt.save(nil) }
就这样!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。