ios - controllerDidChangeContent 内的 EXC_BAD_ACCESS : at line tableView. endUpdates()

标签 ios swift uitableview nsfetchedresultscontroller

这是我的全部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/

相关文章:

ios - Swift:无法长按拖动单元格到 UICollectionViewController 和 UITableView 中的空白部分

ios - 我的应用程序正在请求 “Have offline access” 的权限,为什么?

ios - UILabel 文本即使具有新值也不会更新,已尝试一切

ios - 按下表格单元格时将 View 加载到导航 View Controller 中

ios - 在 UIPageViewController 内的 ViewController 内的 ScrollView 中检测 ScrollOffset?

ios - 当我尝试启动前置摄像头时出现错误 (Swift)

ios - 将使用两者之一。哪一个是未定义的。从 TableView 导航到 Web View

ios - uicollectionview 滚动更改单元格索引路径

iphone - 如何检测 View 中任意位置的点击?

ios - Swift NSDateFormatter 没有使用正确的语言环境和格式