在UITableView Bug&中重新排列单元格保存更改 [英] Rearranging Cells in UITableView Bug & Saving Changes

查看:34
本文介绍了在UITableView Bug&中重新排列单元格保存更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自定义 UITableView 中重新排列单元格时,我遇到了一个错误,例如

When rearranging cells in my custom UITableView I am encountering a bug where for e.g.

如果我重新排列1,2,3,4,5并将单元格1移动到单元格5的末尾,那么所有单元格将自己重命名为要替换的单元格.因此,代替新的阶数为2,3,4,5,1,它变为1(2),2(3),3(4),4(5),1(移动单元).

If I am rearranging 1,2,3,4,5 and I move cell 1 to the end where cell 5 is then what happens is all the cells rename themselves to whichever cell they are replacing. So instead of the new order being 2,3,4,5,1 it becomes 1(2),2(3),3(4),4(5),1(moved cell).

在我的核心数据模型中,我有两个实体,一个是文件夹,另一个是商品.它是 NSSet 类型的多对多关系,因此Folder可以具有许多Items.物料具有名为 Int 类型的名为 index 的属性,并且表示数组中的物料顺序&;文件夹具有称为 arrayOfItems 的临时属性,该属性的类型可转换.这将返回按其索引值排序的Item数组.

In my core data model I have two entities one for Folder and another for Items. It is a to many relationship of type NSSet so Folder can have many Items. Items have a property called index of type Int and represents the items order in the array & the Folder has transient property called arrayOfItems which of type transformable. This returns an array of the Items ordered by their index value.

控制台会打印出didmovecell功能的结尾 ,但不会打印出 Move cell .

The console prints out end of didmovecell function but not Moved cell.

table.didMoveCellFromIndexPathToIndexPathBlock = {(fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) -> Void in

   func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
   let itemThatMoved = self.selectedFolder.arrayOfItems[sourceIndexPath.row]
   self.selectedFolder.arrayOfItems.removeAtIndex(sourceIndexPath.row)
   self.selectedFolder.arrayOfItems.insert(itemThatMoved, atIndex:destinationIndexPath.row )

    var currentIndex = 0
    for item in self.selectedFolder.arrayOfItems {
     item.index=currentIndex
     currentIndex++

     println("Moved cell")
    }
   }
 println("end of didmovecell function")
}

任何可能会发生这种情况的想法,以及如何将更改保存到核心数据模型的任何想法,例如更新项目索引值.

Any ideas why this might be happening and how I can save the changes to my core data model e.g. updating the item Index value.

PS:我已经为自定义UITableView 测试了 didMoveCellFromIndexPathToIndexPathBlock ,而没有使用核心数据和名为Objects的基本数组.像这样的东西工作得很好.

PS: I have tested didMoveCellFromIndexPathToIndexPathBlock for my custom UITableView without using core data with a basic array called Objects. It worked fine with something like this;

 table.didMoveCellFromIndexPathToIndexPathBlock = {(fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) -> Void in
    self.objects.exchangeObjectAtIndex(toIndexPath.row, withObjectAtIndex: fromIndexPath.row)
  }

推荐答案

好,如果您需要保留这些块,则将代码更改为:

Ok, if you need to keep the blocks then change your code to:

table.didMoveCellFromIndexPathToIndexPathBlock = {(fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) -> Void in

   let itemThatMoved = self.selectedFolder.arrayOfItems[fromIndexPath.row]
   self.selectedFolder.arrayOfItems.removeAtIndex(fromIndexPath.row)
   self.selectedFolder.arrayOfItems.insert(itemThatMoved, atIndex:toIndexPath.row )

   var currentIndex = 0
   for item in self.selectedFolder.arrayOfItems {
      item.index=currentIndex
      currentIndex++
   }

   // at this point call saveContext() to ensure that rearrangement is saved in Core Data
}

以前,您的代码块中包装了一个函数.您正在调用该块,但从未调用过其中的函数:)

Previously you had a function wrapped in your block. You were calling the block but the function within it was never called :)

这篇关于在UITableView Bug&中重新排列单元格保存更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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