使用NSFetchedResultsController移动行的方法及其代理不起作用 [英] Method for moving rows with NSFetchedResultsController and its delegate not working
问题描述
我一直在尝试按照此教程进行重新整理使用NSFetchedResultsController和NSFetchedResultsController委托时在表视图中显示行,但是我的项目存在很多错误。发生的是,当我尝试重新排列一行并将其放下时,这些行是随机的(可能不是随机的,但是我看不到找到问题的模式)。我的实现有什么问题?
I've been trying to follow this tutorial for rearranging rows in a table view while using NSFetchedResultsController and NSFetchedResultsController delegate, but my project is extremely buggy. What happens is that when I try rearranging a row and drop it, the rows are randomised (probably not randomised, but I can't see the pattern to find the problem). What's wrong with my implementation?
表视图数据源:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("RoutineCell", forIndexPath: indexPath) as! UITableViewCell
var routine = fetchedResultsController.objectAtIndexPath(indexPath) as! Routine
lastIndex++
// Configure the cell...
cell.textLabel?.text = routine.name
return cell
}
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext!
var routine = fetchedResultsController.objectAtIndexPath(indexPath) as! Routine
// Delete the row from the data source
managedContext.deleteObject(routine)
managedContext.save(nil)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext!
if var routines = fetchedResultsController.fetchedObjects {
let routine = routines[sourceIndexPath.row] as! Routine
println("Instance of routine: \(routines[sourceIndexPath.row].description) created")
routines.removeAtIndex(sourceIndexPath.row)
println("Fetched data is: \(routines.description)")
println("Routine removed at \(routines[sourceIndexPath.row])")
routines.insert(routine, atIndex: destinationIndexPath.row)
println("Routine inserted at index \(destinationIndexPath.row)")
var idx: Int = Int(routines.count)
for routine in routines as! [Routine] {
routine.index = idx--
}
managedContext.save(nil)
}
dispatch_async(dispatch_get_main_queue(), { () -> Void in
tableView.reloadRowsAtIndexPaths(tableView.indexPathsForVisibleRows()!, withRowAnimation: UITableViewRowAnimation.Fade)
})
}
NSFetchedResultsControllerDelegate方法:
NSFetchedResultsControllerDelegate methods:
func controllerWillChangeContent(controller: NSFetchedResultsController) {
self.tableView.beginUpdates()
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
}
func controller(
controller: NSFetchedResultsController,
didChangeObject anObject: AnyObject,
atIndexPath indexPath: NSIndexPath?,
forChangeType type: NSFetchedResultsChangeType,
newIndexPath: NSIndexPath?) {
// implementation to follow...
switch type {
case .Insert:
// Note that for Insert, we insert a row at the __newIndexPath__
if let insertIndexPath = newIndexPath {
self.tableView.insertRowsAtIndexPaths([insertIndexPath], withRowAnimation: .Fade)
}
case .Delete:
// Note that for Delete, we delete the row at __indexPath__
if let deleteIndexPath = indexPath {
self.tableView.deleteRowsAtIndexPaths([deleteIndexPath], withRowAnimation: .Fade)
}
case .Update:
// Note that for Update, we update the row at __indexPath__
if let updateIndexPath = indexPath {
let cell = self.tableView.cellForRowAtIndexPath(updateIndexPath)
let routine = self.fetchedResultsController.objectAtIndexPath(updateIndexPath) as? Routine
cell?.textLabel?.text = routine?.name
}
case .Move:
// Note that for Move, we delete the row at __indexPath__
if let deleteIndexPath = indexPath {
self.tableView.deleteRowsAtIndexPaths([deleteIndexPath], withRowAnimation: .Fade)
}
// Note that for Move, we insert a row at the __newIndexPath__
if let insertIndexPath = newIndexPath {
self.tableView.insertRowsAtIndexPaths([insertIndexPath], withRowAnimation: .Fade)
}
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
self.tableView.endUpdates()
}
我做的动作与上面的gif相同:
I did the same moves as in the gif above:
推荐答案
您更新数据模型的例程有点难以理解。您正在为列表顶部的记录分配最高的索引
,倒数到 1
并可能反向排序。 OK,让我们假设它正在工作。 *)
Your routine updating the data model is a bit difficult to read. You are assigning the highest index
to the record on top of the list counting down to 1
and presumably reverse sorting... OK, let's assume that is working. *)
也许您应该在处理用户生成的数据更改时简单地禁用委托方法?用$ p $
Perhaps you should simply disable the delegate methods when you process user generated changes of the data? Bracket your operations including the save with
self.fetchedResultsController.delegate = nil
// ...
self.fetchedResultsController.delegate = self
*)我认为如果您最初是在数数向上,然后使用获取的结果控制器对向下计数进行重新排序,您将得到图像中所示的那种错误。
这篇关于使用NSFetchedResultsController移动行的方法及其代理不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!