如何关闭 UISearchBar 中的实时搜索

发布于 2024-11-03 06:01:09 字数 233 浏览 1 评论 0原文

我已经使用 UIViewController、UISearchBarDelegate、UISearchDisplayDelegate 实现了 SearchBar,当执行搜索时,我的程序向服务器发送 http 请求,然后解析响应正文。这将导致搜索栏在每个字符输入后延迟几秒钟。

所以我想关闭搜索栏的“实时搜索”,这样它就不会在我每次输入字符时都执行搜索。当我单击键盘的“搜索”按钮时,我还想执行搜索并将数据显示到表格视图中。我能做些什么?

I have implemented a SearchBar using an UIViewController, UISearchBarDelegate, UISearchDisplayDelegate, and when the search performs, my program send a http request to the server and then parser the response body. And this will cause the search bar delay for a few second after each character typed.

So I want to turn off the "live search" of the search bar so that it will not perform search every time I type a character. And also I want to perform search and display the data to the tableview when I click the "Search" button of the keyboard. What can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹤舞 2024-11-10 06:01:09

调用 [self.searchDisplayController.searchResultsTableView reloadData];
仅当您的搜索请求已完成时,并且仅在调用委托 - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 时才发出搜索请求。

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{

    DLog(@"search text is %@",searchBar.text);

    [self makeSearchRequest:searchBar.text];
}

-(void)makeSearchRequest:(NSString*)pString
{
    CLLocation *location = [[AppHelper appDelegate] mLatestLocation];

    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc] init];
    [paramDic setValue:[[AppHelper mDataManager] objectForKey:_KEY(KEY_USERID)] forKey:@"userid"];
    [paramDic setValue:@"H0001" forKey:@"client"];
    [paramDic setValue:[[AppHelper mDataManager] objectForKey:_KEY(KEY_LOCATION_ID)] forKey:@"locationid"];
    [paramDic setValue:@"0" forKey:@"pageid"];
    [paramDic setValue:@"10" forKey:@"displayrecords"];
    //  [paramDic setValue:[[dic objectForKey:@"birthday"] stringByReplacingOccurrencesOfString:@"/" withString:@"-"] forKey:@"dateofbirth"];
    **[paramDic setValue:pString forKey:@"keyword"];**




    [self.mWNetowrk makeRequsetWithURL:URL_SEARCH type:ReqSearch paramDictionary:paramDic delegate:self];
    [paramDic autorelease];
}

-(void)network:(WNetwork*)network didFinishLoadingWithRequest:(NSInteger)pReq data:(NSMutableDictionary*)pData
{
    [self removeLoader  ];

    switch (pReq) {

        case ReqSearch:
            self.mArrayPlaces = [pData objectForKey:@"places"];
            [mPlacesCacheArray release];
            mPlacesCacheArray=nil;
            if (!mPlacesCacheArray) {

                mPlacesCacheArray =[NSMutableArray new];
                for (int i =0 ; i<[mArrayPlaces count]; i++) {
                    [mPlacesCacheArray addObject:[NSNull null]];

                }
            }

            [self.searchDisplayController.searchResultsTableView reloadData];

            break;

        default:
            break;
    }

}

call [self.searchDisplayController.searchResultsTableView reloadData];
only when your search request has been completed and make the search request only when the delegate - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar is called.

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{

    DLog(@"search text is %@",searchBar.text);

    [self makeSearchRequest:searchBar.text];
}

-(void)makeSearchRequest:(NSString*)pString
{
    CLLocation *location = [[AppHelper appDelegate] mLatestLocation];

    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc] init];
    [paramDic setValue:[[AppHelper mDataManager] objectForKey:_KEY(KEY_USERID)] forKey:@"userid"];
    [paramDic setValue:@"H0001" forKey:@"client"];
    [paramDic setValue:[[AppHelper mDataManager] objectForKey:_KEY(KEY_LOCATION_ID)] forKey:@"locationid"];
    [paramDic setValue:@"0" forKey:@"pageid"];
    [paramDic setValue:@"10" forKey:@"displayrecords"];
    //  [paramDic setValue:[[dic objectForKey:@"birthday"] stringByReplacingOccurrencesOfString:@"/" withString:@"-"] forKey:@"dateofbirth"];
    **[paramDic setValue:pString forKey:@"keyword"];**




    [self.mWNetowrk makeRequsetWithURL:URL_SEARCH type:ReqSearch paramDictionary:paramDic delegate:self];
    [paramDic autorelease];
}

-(void)network:(WNetwork*)network didFinishLoadingWithRequest:(NSInteger)pReq data:(NSMutableDictionary*)pData
{
    [self removeLoader  ];

    switch (pReq) {

        case ReqSearch:
            self.mArrayPlaces = [pData objectForKey:@"places"];
            [mPlacesCacheArray release];
            mPlacesCacheArray=nil;
            if (!mPlacesCacheArray) {

                mPlacesCacheArray =[NSMutableArray new];
                for (int i =0 ; i<[mArrayPlaces count]; i++) {
                    [mPlacesCacheArray addObject:[NSNull null]];

                }
            }

            [self.searchDisplayController.searchResultsTableView reloadData];

            break;

        default:
            break;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文