How to prevent from scrolling UITableView up when NSFetchedResultsController add new record?

前端 未结 2 1112
清酒与你
清酒与你 2020-12-19 14:23

My UITableView is scrolling up once I add a new comment to CoreData. NSFetchedResultsController knows about it, puts this comment at the bottom of the table, an

相关标签:
2条回答
  • 2020-12-19 14:36

    I found something that worked for me. I encountered the same problem, and my estimatedRowHeight was 33 and all of my cells are greater than 33.

    I just changed my estimatedRowHeight to my maximum cell height(or greater than your maximum cell height)

        self.table.estimatedRowHeight = 310
        self.table.rowHeight = UITableViewAutomaticDimension
    

    worked like a charm, all cells are now autoresizing and uitableview is not scrolling to top or bottom when core data performs an update/delete/insert

    0 讨论(0)
  • 2020-12-19 14:51

    Edited answer:

    I was able to reproduce this glitch with the following steps:

    1. UITableView with its rowHeight set to UITableViewAutomaticDimension and non-zero estimatedRowHeight.
    2. Table scrolled to its very bottom, with the last cell fully visible at the bottom margin.
    3. Inserting the new cell below the last one results in visual shift of the cells a few points down (in my setup it differs from 5 to 40).

    The source of this behavior needs additional investigation.

    For now the only solution is not to use UITableViewAutomaticDimension and return row heights from tableView:heightForRowAtIndexPath:.

    By the way, implementation of inverted index paths has one significant flaw. When FRC calls it's delegate method controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:, all deletion index paths belongs to pre-change data set, and all insertion index paths - to post-change one. When you are in-beetween controllerWillChangeContent: and controllerDidChangeContent:, fetchedResultsController.fetchedObjects will contain post-change data set, i suppose (needs to be checked, though).

    0 讨论(0)
提交回复
热议问题