簡體   English   中英

UITableView意外地與beginUpdates()/ endUpdates()/ performBatchUpdates()反彈

[英]UITableView unexpectedly bounces with beginUpdates()/endUpdates()/performBatchUpdates()

我在這里有一個非常簡單的UITableViewController / NSFetchedResultsController案例。 它來自Xcode Master-Detail App示例代碼,因此易於重現。
我有一個CoreData模型,帶有1個實體,帶有1個字符串屬性。 它顯示在UITableViewController 我使用.subtitle系統單元格類型。 通過選擇一行,我只需更新字符串屬性。
所以我的問題是,當我為tableview添加足夠的行來滾動(帶導航欄的iPhone 5s上的10-11行),然后向下滾動,並選擇任何行,tableview上下反彈。
如果行(少於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()
  }
}

我遇到了同樣的問題,我發現實現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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM