gpt4 book ai didi

xcode - 使用 tableView :moveRowAtIndexPath:toIndexPath: method 后将新订单保存到核心数据

转载 作者:搜寻专家 更新时间:2023-10-31 21:55:46 24 4
gpt4 key购买 nike

我在 Swift iOS 应用程序中有一个 tableView,它允许用户对行重新排序。点击“编辑”按钮,可以重新排序或删除行,然后再次点击“编辑”按钮(现在显示为“完成”)以完成该过程。

如果我只是使用 tableView:moveRowAtIndexPath:toIndexPath: 方法,它会按上述方式工作,并且当点击“完成”时,表格会正确显示重新排序的行。但是它在返回表时不记得新的顺序,所以我在核心数据中添加了一个新的“位置”变量 (Int),并根据它让我的表自行排序。

我的问题是,在用户移动一行并单击“完成”后,表格会立即自行重新排序,恢复到之前的状态。所以它在核心数据中使用旧的(原始的)位置变量,然后我的代码才能正确捕获新订单并将其重新写入核心数据。

有没有办法在表格重新加载前点击“完成”时捕获这个新订单?

这是我处理移动的代码:

func tableView(tableView: UITableView!, moveRowAtIndexPath sourceIndexPath: NSIndexPath!, toIndexPath destinationIndexPath: NSIndexPath!) {
}

这是我在按下“完成”时调用的代码。我遍历表格的每一行,并根据新行在核心数据中重新分配人员的位置变量:

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();
}

当按下 Done 按钮时调用上面的代码,但我也尝试在 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...

在此先感谢您提供的任何帮助。

最佳答案

  1. 添加到 CoreData“orderPosition”类型的 Int 属性并将 orderPosition 添加到您的模型文件。

  2. 当您向 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
  3. 在您的 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)!

    }
  4. 记住,在将他发送到表之前,您必须始终对数组进行排序!

  5. 如果要对表格行重新排序,您必须做什么? :

    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")


    }

    MyData[fromIndexPath.row].setValue(toIndexPath.row, forKey: "orderPosition")



    }

    contxt.save(nil)

    RefreshCoredata()

    }

这需要在您的 CoreData 中重置排序位置

  1. 当你想删除一些表格行时,你必须做什么?

    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)

    }

就是这样!

关于xcode - 使用 tableView :moveRowAtIndexPath:toIndexPath: method 后将新订单保存到核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28413081/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com