UISearchDisplayContoller – can't prevent table reload on typing in search bar

谁都会走 提交于 2019-11-28 07:48:25

This is just the way UISearchDisplayController (SDC) works. When the user enters the first character into the searchBar the searchTable is loaded and displayed for the first time causing it to load. The methods "...shouldReloadTableForSearchString" and "...shouldReloadTableForSearchScope" allow you to control whether the searchTable reloads automatically on subsequent chars or a scope change.

I've done both of the following to provide a good user experience on the first character. Slight disclaimer: I do have implementations of both of these that work but this is simply a framework for implementation from my memory. I may have missed a detail but this should get you pretty close.

Option 1: Present a "loading" cell in the searchTable when the first char is typed.

This option allows the SDC to display the searchResultsTableView when the user types the first char, display status as to the current search/filter operation

  1. in the SDC delegate class definition

    • add the iVar BOOL isLoading
    • add the iVar UITableView *searchTableView
  2. in searchDisplayController:didLoadSearchResultsTableView

    • set searchTableView = tableView
  3. in shouldReloadTableForSearchString/Scope

    • set isLoading = YES
    • call your method to load data in the background
    • return NO
  4. when your background filter is complete:

    • set isLoading = NO
    • [searchTableView reloadData]
  5. in the various tableView delegate methods respond how you like to show status if there are current search results or results are loading in the background. What I did is:

    • if there are current search results, show results (even if loading/filtering in the background)
    • if there are no search results and isLoading == NO return 1 row and show 'No matches' in a cell
    • if there are no search results and isLoading == YES return 1 row and and show search activity in a cell (I typically use UIActivityIndicatorView)

Option 2: Hide the searchTableView and display an overlay view in it's place until search results are loaded

This option hides the searchTableView when it is first loaded and only redisplays it if when the search/filter is complete. I defined this as an add on to option 1 as they can be done together though to optimize things you may not care about showing search activity in the searchResultsTableView if you are hiding the table and showing the overlay.

  1. in the SDC delegate class definition

    • same as Option 1
    • add the iVar UIView *searchTableOverlayView
  2. in searchDisplayController:didLoadSearchResultsTableView

    • same as Option 1
    • create a UIView to use as an overlay in place of searchTableView containing whatever UI is appropriate for your app and set it to searchTableOverlayView
  3. in searchDisplayController:didUnloadSearchResultsTableView

    • release searchTableOverlayView
  4. in 'searchDisplayController:didShowSearchResultsTableView(may be able to do this insearchDisplayController:willShowSearchResultsTableView`

    • if there are search results to display or isLoading == NO
      • seachTableOverlayView.hidden == YES
    • else (if isLoading == YES)
      • searchTableOverlayView.frame == searchResultsTableView.frame
      • add seachTableOverlayView as a subview of searchTableVIew.superview
      • searchTableView.hidden = YES
  5. when your background filter is complete

    • same as option 1
    • if there are searchResults to display
      • searchTableCoverView.hidden = YES
      • searchResultsTableView.hidden = NO
    • else
      • searchResultsTableView.hidden = YES
      • searchTableCoverView.hidden = NO
  6. in the various tableView delegate methods respond how you like to show status if there are current search results or results are loading in the background. What I did is:

    • same as option 1

Unfortunately I believe this is just how the UISearchDisplayController functions. You should file a bug report to Apple if you want to request different options for functionality.

The alternative would be to write your own UISearchBar and UITableView combination similar to how UISearchDisplayController works, then you'll get better control.

Hope this helps!

When I faced the same issue I tried to put an overlay view on search result view. That didn't help me. Looks like Apple change the behavior and I saw cell separators shone through my view. Finally I came up with a good approach.

At first my async search class had it's own overlay, progress and other views. UISearchDisplayController was used only for start, end delegate methods calls and sticking to default search flow. To show view I used any subview I can put a search views on (as an init call):

newView.frame = frame;
[view addSubview:newView];

and then use the subview when changing my search progress views:

if (view == currentView)
    return;

view.frame = currentView.frame;
view.alpha = [self getAlphaValueForView:view];

UIView * superView = [currentView superview];
[currentView removeFromSuperview];

currentView = view;
[superView addSubview:currentView];

The problem with empty search result table view blinking appears when I start to use my search content controller in default navigation routine (navigation controller).

So to solve the problem I start using for navigation not a content controller (in this case the default search display controller activating) but an empty controller. And I used empty controllers view as a subview for search views.

So in total: DO NOT use UISearchDisplayController's content view controller in navigation. Use stub view controller. And then use it's view to put your views as subviews.

Worked great for me.

Hide self.searchDisplayController.searchResultsTableView until user presse search button

- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView;
{
    tableView.hidden = YES;
}

After user press the search key, and the search results get ready, make the search result table visible

self.searchDisplayController.searchResultsTableView.hidden = NO;
[self.searchDisplayController reloadData]

This workaround works for me.

Stan

Maybe you need to implement UISearchDisplayController and then this function:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!