xcode – 使用tableView后将新订单保存到核心数据:moveRowAtInd
我在Swift iOS应用程序中有一个tableView,允许用户重新排序行.点击编辑按钮,可以重新排序或删除行,并重新点击编辑按钮(现在显示为完成)以完成该过程.
如果我只是使用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”类型的Int属性,并将orderPosition添加到您的Model文件中.
>向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) } 就这样! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |