这是我的全部NSFetchedResultsControllerDelegate
:
//MARK: - NSFetchedResultsControllerDelegate
func controllerWillChangeContent(controller: NSFetchedResultsController) {
self.tableView.beginUpdates()
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case .Insert:
if let newIndexPath = newIndexPath {
tableView.insertRowsAtIndexPaths([reversedIndexPathForIndexPath(newIndexPath)], withRowAnimation: .Fade)
}
case .Delete:
if let indexPath = indexPath {
tableView.deleteRowsAtIndexPaths([reversedIndexPathForIndexPath(indexPath)], withRowAnimation: .Fade)
}
case .Update:
if let indexPath = indexPath {
if let cell = tableView.cellForRowAtIndexPath(reversedIndexPathForIndexPath(indexPath)) as? WLCommentTableViewCell {
updateCell(cell, withIndexPath: indexPath)
}
}
case .Move:
if let indexPath = indexPath, let newIndexPath = newIndexPath {
tableView.deleteRowsAtIndexPaths([reversedIndexPathForIndexPath(indexPath)], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([reversedIndexPathForIndexPath(newIndexPath)], withRowAnimation: .Fade)
}
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
tableView.endUpdates() //Thread 1: EXC_BAD_ACCESS (code=1, address=0x20)
updateView()
if shouldScrollTableToBottom {
scrollTableViewToBottom()
}
}
有时我的应用程序会在 tableView.endUpdates()
处崩溃。为什么?
最佳答案
线
tableView.endUpdates() //Thread 1: EXC_BAD_ACCESS (code=1, address=0x20)
应该进入 NSFetchresultsController 委托(delegate)方法,通知您所有更改已完成:
controllerDidChangeContent: Notifies the receiver that the fetched results controller has completed processing of one or more changes due to an add, remove, move, or update.
func controllerDidChangeContent(controller: NSFetchedResultsController) {
updateView()
if shouldScrollTableToBottom {
scrollTableViewToBottom()
}
tableView.endUpdates() // Mark here no more updates to the tableview
}
endUpdates: Concludes a series of method calls that insert, delete, select, or reload rows and sections of the table view. You call this method to bracket a series of method calls that begins with beginUpdates and that consists of operations to insert, delete, select, and reload rows and sections of the table view. When you call endUpdates, UITableView animates the operations simultaneously. Invocations of beginUpdates and endUpdates can be nested. If you do not make the insertion, deletion, and selection calls inside this block, table attributes such as row count can become invalid.
关于ios - controllerDidChangeContent 内的 EXC_BAD_ACCESS : at line tableView. endUpdates(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34392876/