如果我只是使用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 AppDelegatelet 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 AppDelegatelet 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) }
就这样!
总结以上是内存溢出为你收集整理的xcode – 使用tableView后将新订单保存到核心数据:moveRowAtIndexPath:toIndexPath:method全部内容,希望文章能够帮你解决xcode – 使用tableView后将新订单保存到核心数据:moveRowAtIndexPath:toIndexPath:method所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)