iOS:表视图开始更新从不播放动画



我编写了一些代码,当用户长按单元格并确认更改时,从后端和本地数据存储中删除帖子。但是,表视图不会更新。 我已经中断了我的代码并且执行了 tableView 操作代码,为什么我的行没有从视图中删除?

let alertView = UIAlertController(title: "Really?", message: "Are you sure you want to delete this post? Once you do it will be gone forever.", preferredStyle: .Alert)
    alertView.addAction(UIAlertAction(title: "Delete Post", style: UIAlertActionStyle.Default, handler: { (alertView:UIAlertAction!) -> Void in
        let p = post.getRawPost()
        p.deleteInBackgroundWithBlock({ (deleted:Bool, error:NSError?) -> Void in
            // Remove from the local datastore too
            p.fetchFromLocalDatastore()
            p.deleteInBackgroundWithBlock({ (deleted:Bool, error:NSError?) -> Void in
                // Update the table view
                self.tableView.beginUpdates()
                self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Left)
                self.tableView.endUpdates()
        })
    })
}))

据我了解,beginUpdatesendUpdates 用于更新 tableView 的特定部分,而无需对源执行完整的reloadData调用。 我在这里错过了什么吗?

更新 我将 tableView 更新代码移动到主线程,每当调用 endUpdates 时都会产生崩溃。 我的新代码如下所示,并在主线程上调用:

self.tableView.beginUpdates()
self.tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: indexPath.row, inSection: indexPath.section)], withRowAnimation: UITableViewRowAnimation.Automatic)
self.tableView.endUpdates()

将下面的代码片段包装在主线程中:

self.tableView.beginUpdates()
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Left)
self.tableView.endUpdates()

编辑崩溃

p.deleteInBackgroundWithBlock({ (deleted:Bool, error:NSError?) -> Void in
   // Remove from the local datastore too
   p.fetchFromLocalDatastore()
   // Delete the desired item from array
   ...
   // Remove cell that the item belong to animated in main thread
   dispatch_async(dispatch_get_main_queue(), {
       // Update the table view
       self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Left)
   })
})

最新更新