UITableView 意外地用 beginUpdates()/endUpdates()/performBatchUpdates() 弹起

nou*_*tzi 5 core-data uitableview nsfetchedresultscontroller ios

我这里有一个非常简单的UITableViewController案例NSFetchedResultsController。它来自 Xcode Master-Detail App 示例代码,因此很容易重现。
我有一个 CoreData 模型,其中有 1 个实体和 1 个字符串属性。它显示在一个UITableViewController. 我使用.subtitle系统单元类型。通过选择一行,我只需更新字符串属性。
所以我的问题是,当我添加足够的行供表格视图滚动时(带导航栏的 iPhone 5s 上的 10-11 行),然后向下滚动并选择任何行,表格视图会上下弹跳。
如果行数较少(少于 10 行)或多行(12 行及以上),则行为正常。
所以问题似乎是在滚动视图的极限处发生的。如果我不使用beginUpdates()/ endUpdates(),问题就会消失,但我会失去它们的优势。 这是发生的情况
的视频链接

class TableViewController: UITableViewController, NSFetchedResultsControllerDelegate {

  var managedObjectContext: NSManagedObjectContext? = nil

  @objc func insertNewObject(_ sender: Any) {
    let context = self.fetchedResultsController.managedObjectContext
    let newEvent = Event(context: context)
    newEvent.aString = "a String"
    try? context.save()
  }

  override func viewDidLoad() {
    super.viewDidLoad()

    let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(insertNewObject(_:)))
    navigationItem.rightBarButtonItem = addButton
  }

  override func numberOfSections(in tableView: UITableView) -> Int {
    return fetchedResultsController.sections?.count ?? 0
  }

  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let sectionInfo = fetchedResultsController.sections![section]
    return sectionInfo.numberOfObjects
  }

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    let event = fetchedResultsController.object(at: indexPath)
    configureCell(cell, withEvent: event)
    return cell
  }

  func configureCell(_ cell: UITableViewCell, withEvent event: Event) {
    cell.textLabel?.text = event.aString
    cell.detailTextLabel?.text = event.aString
  }

  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    let event: Event = self.fetchedResultsController.object(at: indexPath)
    event.aString = event.aString! + ""
  }

  override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
  }

  override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
      let context = fetchedResultsController.managedObjectContext
      context.delete(fetchedResultsController.object(at: indexPath))
      try? context.save()
    }
  }

  var fetchedResultsController: NSFetchedResultsController<Event> {
    if _fetchedResultsController != nil {
      return _fetchedResultsController!
    }

    let fetchRequest: NSFetchRequest<Event> = Event.fetchRequest()
    fetchRequest.fetchBatchSize = 20
    let sortDescriptor = NSSortDescriptor(keyPath: \Event.aString, ascending: false)
    fetchRequest.sortDescriptors = [sortDescriptor]

    let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: nil, cacheName: nil)
    aFetchedResultsController.delegate = self
    _fetchedResultsController = aFetchedResultsController
    try? _fetchedResultsController!.performFetch()

    return _fetchedResultsController!
  }

  var _fetchedResultsController: NSFetchedResultsController<Event>? = nil

  func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    tableView.beginUpdates()
  }

  func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
    switch type {
    case .insert:
      tableView.insertSections(IndexSet(integer: sectionIndex), with: .automatic)
    case .delete:
      tableView.deleteSections(IndexSet(integer: sectionIndex), with: .automatic)
    default:
      return
    }
  }

  func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
    switch type {
    case .insert:
      tableView.insertRows(at: [newIndexPath!], with: .automatic)
    case .delete:
      tableView.deleteRows(at: [indexPath!], with: .automatic)
    case .update:
      configureCell(tableView.cellForRow(at: indexPath!)!, withEvent: anObject as! Event)
    case .move:
      configureCell(tableView.cellForRow(at: indexPath!)!, withEvent: anObject as! Event)
      tableView.moveRow(at: indexPath!, to: newIndexPath!)
    }
  }

  func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    tableView.endUpdates()
  }
}
Run Code Online (Sandbox Code Playgroud)

ada*_*ulf 1

我遇到了同样的问题,我发现实施这些estimatedHeightFor...方法可以解决我的问题。该问题似乎源于在表中使用自动单元格高度而不是明确定义的每行高度。

我使用的表格既有行又有节页眉/页脚,因此我需要定义行和页眉的估计高度,这解决了批量更新期间奇怪的弹跳动画。

请注意,为节标题高度返回 0 将使用表视图的默认值(很可能)UITableViewAutomaticDimension,因此为估计高度返回正数。

我在 Obj-C 中的代码:

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44; // anything except 0 or UITableViewAutomaticDimension
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section{
    return 18; // anything except 0 or UITableViewAutomaticDimension
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section{
    return 18; // anything except 0 or UITableViewAutomaticDimension
}
Run Code Online (Sandbox Code Playgroud)