2015-12-21 89 views
0

这是我所有的NSFetchedResultsControllerDelegateEXC_BAD_ACCESS:在line tableView.endUpdates()

//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()行崩溃。为什么?

+0

你确定你的数据源的数量,我的意思是更新前和更新后的行数是相同的。请检查一下,这可能会有所帮助。 – Sabby

+0

我很肯定他们是一样的:)我关注这个 –

回答

1

线

tableView.endUpdates() //Thread 1: EXC_BAD_ACCESS (code=1, address=0x20) 

应该通知您的所有更改NSFetchresultsController委托方法内完成:

controllerDidChangeContent:通知接收器获取的成果控制器完成的处理 的一个或多个更改添加,删除,移动或更新。

func controllerDidChangeContent(controller: NSFetchedResultsController) { 
    updateView() 

    if shouldScrollTableToBottom { 
     scrollTableViewToBottom() 
    } 
    tableView.endUpdates() // Mark here no more updates to the tableview 
} 

endUpdates:圆满结束一系列方法调用插入,删除,选择或重新加载该表视图的行和段。 您可以调用此方法以包围以beginUpdates开头的一系列方法调用,该方法调用由插入,删除,选择和重新加载表视图的行和部分的操作组成。当您调用endUpdates时,UITableView将同时为这些操作提供动画。 beginUpdates和endUpdates的调用可以嵌套。 如果您不在该块内进行插入,删除和选择调用,则表格属性(如行计数)可能会变为无效

+0

那么,我的代码有什么问题? –

+0

将endUpdates想象成一种转换完成方法:在设置插入,移动或删除单元格时调用它。 –