Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
502 views
in Technique[技术] by (71.8m points)

uitableview - UISearchDisplayController animate reloadData

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 the search criteria change.

I'm using these two methods : UISearchDisplayController methods

They both return YES but still can't find any way to do something similar to : UITableView Methods

I don't know if it's important but I'm using an NSfetchedResultsController?to populate the UITableView in the UISearchDisplayController

That's it thanks !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...