重新排列UITableView中的单元格Bug和保存更改
重新排列单元格在我的自定义 UITableView
我遇到一个错误,例如
如果我正在重新排列1,2,3,4,5,并且将单元格1移动到单元格5的末尾,那么会发生什么情况,所有单元格都会将其自己重命名为要replace的单元格。 因此,代替2,3,4,5,1的新订单,它变成1(2),2(3),3(4),4(5),1(移动单元格)。
在我的核心数据模型中,我有两个用于Folder的实体,另一个用于Items 。 这是一个NSSet
types的很多关系,所以Folder可以有很多项目。 项目有一个叫做Int
typesindex
的属性, 表示数组中的项目顺序,而Folder具有一个名为arrayOfItems
transienttypes的可转换types的transient属性。 这将返回按其索引值sorting的项目的数组。
控制台打印end of didmovecell function
但不 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") }
任何想法,为什么这可能会发生,以及如何我可以保存更改到我的核心数据模型,例如更新项目索引值。
PS:我已经testingdidMoveCellFromIndexPathToIndexPathBlock
为我的自定义UITableView, 而不使用核心数据与基本数组称为对象。 它用这样的东西很好,
table.didMoveCellFromIndexPathToIndexPathBlock = {(fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) -> Void in self.objects.exchangeObjectAtIndex(toIndexPath.row, withObjectAtIndex: fromIndexPath.row) }
好吧,如果你需要保留块,那么改变你的代码:
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 }
以前,你有一个function包裹在你的块。 你正在调用该块,但其中的function从来没有被称为:)