UISearchDisplayContoller - 在搜索栏中键入时无法阻止表重新加载 [英] UISearchDisplayContoller – can't prevent table reload on typing in search bar

查看:106
本文介绍了UISearchDisplayContoller - 在搜索栏中键入时无法阻止表重新加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置搜索显示控制器来处理来自Web服务的异步结果。我已经掌握了基本的位,但遇到了一个我无法弄清楚的奇怪问题。

I'm trying to set up a search display controller to handle async results from a web service. I've got the basic bits in place but have run into a really strange issue that I can't figure out.

似乎要为异步操作设置搜索显示控制器,你真的需要做两件事:

Seems like to rig up the search display controller for async you really just need to do two things:


  1. 返回NO
    searchDisplayController:shouldReloadTableForSearchString,

  2. 处理searchBarSearchButtonClicked
    并触发表重新加载$ ​​b $ b我自己。

我正在做这两件事,但我看到的是搜索显示控制器正在重新加载表格即使我按照#1返回NO,第一个字符也会输入到搜索栏中。它不会在输入的后续字符上重新加载。

I'm doing both of these but what I'm seeing is that the search display controller is reloading the table on the first character typed into the search bar even though I'm returning NO as per #1. It doesn't reload on subsequent characters entered.

所以,我的问题是:如何在用户输入时让搜索显示控制器不再尝试重新加载表格? (特别是关于输入的第一个字符)

我已经看到这个问题被提到作为其他几个问题的一部分,但我还没有看到直接答案问题。在我采用一堆UI修改来解决它之前,我想了解发生了什么或我做错了什么。

I've seen this issue mentioned as part of a couple of other questions but I have not seen a direct answer to the problem. I'd like to understand what's going on or what I'm doing wrong before I resort to a bunch of UI mangling to work around it.

这是我的代码的快速提炼,以显示问题。当我运行它并在搜索栏中输入abcde时,在我输入a后,结果显示为#0,a#2等。在我点击搜索按钮之前,它们不会再次更新然后你会看到abcde#0,abcde#1等。当我点击搜索按钮时,所需的结果当然没有任何反应。

Here's a quick distillation of my code to show the issue. When I run this and type "abcde" into the search bar, after I type "a" the results display as "a #0", "a #2", etc. They don't update again until I hit the search button then you see "abcde #0", "abcde #1", etc. Desired result is, of course, nothing happens until I hit the search button.

#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    return NO;
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
    return NO;
}


#pragma mark -
#pragma mark UISearchBarDelegate Methods

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self.searchDisplayController.searchResultsTableView reloadData];

}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.text = [self.searchDisplayController.searchBar.text stringByAppendingFormat:@" #%d", indexPath.row]; 
    return cell;
}

谢谢! (顺便说一句,这是我在这里提出的第一个问题 - 如果我错过任何礼节,请告诉我:)。

Thanks! (btw, this is my first question asked here—please let me know if I miss any points of etiquette :)

推荐答案

这个就是UISearchDisplayController(SDC)的工作方式。当用户将第一个字符输入searchBar时,第一次加载并显示searchTable,导致其加载。方法... shouldReloadTableForSearchString和... shouldReloadTableForSearchScope允许您控制searchTable是否在后续字符或范围更改时自动重新加载。

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.

选项1:当输入第一个字符时,在searchTable中显示加载单元格。

此选项允许SDC在用户键入第一个字符时显示searchResultsTableView,显示当前搜索/过滤操作的状态

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. 在SDC委托类定义中

  1. in the SDC delegate class definition



  • 添加iVar BOOL isLoading

  • 添加iVar UITableView * searchTableView

  • add the iVar BOOL isLoading
  • add the iVar UITableView *searchTableView


  • in searchDisplayController:didLoadSearchResultsTableView



    • set searchTableView = tableView

    • set searchTableView = tableView


  • in shouldReloadTableForSearchString / Scope



    • 设置 isLoading = YES

    • 调用您的方法在后台加载数据

    • 返回NO


  • 当您的后台过滤器完成时:

  • when your background filter is complete:



    • 设置 isLoading = NO

    • [searchTableView reloadData]

    • set isLoading = NO
    • [searchTableView reloadData]


  • 在各种tableView委托方法中回应你喜欢显示状态的方式当前搜索结果或结果正在后台加载。我做的是:

  • 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:



    • 如果有当前搜索结果,则显示结果(即使加载/过滤背景)

    • 如果没有搜索结果且 isLoading ==否返回1行并在单元格中显示No matches

    • 如果没有搜索结果且 isLoading == YES 返回1行,并在单元格中显示搜索活动(我通常使用UIActivityIndi​​catorView)

    • 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)


  • 选项2 :隐藏searchTableView并在其中显示叠加视图,直到加载搜索结果

    此选项在首次加载时隐藏searchTableView并仅重新显示如果搜索/过滤器完成。我将其定义为选项1的附加内容,因为它们可以一起完成,但如果您隐藏表并显示叠加层,则可以优化您可能不关心在searchResultsTableView中显示搜索活动的内容。

    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



    • 与选项1相同

    • 添加iVar UIView * searchTableOverlayView

    • same as Option 1
    • add the iVar UIView *searchTableOverlayView


  • in searchDisplayController:didLoadSearchResultsTableView


    • 与选项1相同

    • 创建一个UIView作为覆盖来代替searchTableView,其中包含适合您的任何UI应用并将其设置为 searchTableOverlayView

    • 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


  • in searchDisplayController:didUnloadSearchResultsTableView



    • release searchTableOverlayView

    • release searchTableOverlayView


  • in'searchDisplayController:didShowSearchResultsTableView (也许可以在 searchDisplayController中执行此操作:willShowSearchResultsTableView`

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



    • 如果要显示搜索结果或 isLoading ==否

      • seachTableOverlayView.hidden = =是

    • if there are search results to display or isLoading == NO
      • seachTableOverlayView.hidden == YES

    • searchTableOverlayView.frame == searchResultsTableView.frame

    • add seachTableOverlayView 作为的子视图searchTableVIew.superview

    • searchTableView.hidden = YES

    • searchTableOverlayView.frame == searchResultsTableView.frame
    • add seachTableOverlayView as a subview of searchTableVIew.superview
    • searchTableView.hidden = YES


  • 当您的后台过滤器完成时

  • when your background filter is complete



    • 与选项1相同

    • 如果有searchResults到dis玩

      • searchTableCoverView.hidden = YES

      • searchResultsTableView.hidden = NO

    • same as option 1
    • if there are searchResults to display
      • searchTableCoverView.hidden = YES
      • searchResultsTableView.hidden = NO

    • searchResultsTableView.hidden = YES

    • searchTableCoverView.hidden = NO

    • searchResultsTableView.hidden = YES
    • searchTableCoverView.hidden = NO


  • 在各种tableView委托方法中响应如果当前搜索结果或结果在后台加载,您希望如何显示状态。我做的是:

  • 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:



    • 与选项1相同


  • 这篇关于UISearchDisplayContoller - 在搜索栏中键入时无法阻止表重新加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆