UISearchDisplayController animate reloadData

后端 未结 2 1464
不思量自难忘°
不思量自难忘° 2020-12-21 12:24

I\'ve been reading all the documentation about UISearchDisplayController and its delegate but I can\'t find any way to animate the table view when

2条回答
  •  礼貌的吻别
    2020-12-21 12:52

    When the search string or scope has changed, you assign a new fetch request for the fetched results controller and therefore have to call performFetch to get a new result set. performFetch resets the state of the controller, and it does not trigger any of the FRC delegate methods.

    So the table view has to be updated "manually" after changing the fetch request. In most sample programs, this is done by

    • calling reloadData on the search table view, or
    • returning YES from shouldReloadTableForSearchString or shouldReloadTableForSearchScope.

    The effect is the same: The search table view is reloaded without animation.

    I don't think there is any built-in/easy method to animate the table view update when the search predicate changes. However, you could try the following (this is just an idea, I did not actually try this myself):

    • Before changing the fetch request, make a copy of the old result set:

      NSArray *oldList = [[fetchedResultsController fetchedObjects] copy];
      
    • Assign the new fetch request to the FRC and call performFetch.

    • Get the new result set:

      NSArray *newList = [fetchedResultsController fetchedObjects];
      
    • Do not call reloadData on the search table view.

    • Compare oldList and newList to detect new and removed objects. Call insertRowsAtIndexPaths for the new objects and deleteRowsAtIndexPaths for the removed objects. This can be done by traversing both lists in parallel.
    • Return NO from shouldReloadTableForSearchString or shouldReloadTableForSearchScope.

    As I said, this is just an idea but I think it could work.

提交回复
热议问题