UISearchDisplayController-等待N秒钟,或者让用户按"Search"(搜索)在进行搜索之前 [英] UISearchDisplayController - wait for N seconds OR for user to press "Search" before conducting search
问题描述
我有一个UISearchDisplayController
,目前我后悔执行.问题是我的搜索控制器访问Web服务,然后通过调用[tableView reloadData]
更新UITableView
.每次用户输入密钥时,它都会进行搜索.通过调用ASIHTTPRequest
至少需要三个字符,我已经能够部分缓解症状,但是必须有更好的方法.基本上,我希望UISearchDisplayController
等待N秒,或者让用户在搜索之前按搜索".这是我现在拥有的:
I have a UISearchDisplayController
that I am currently regretting implementing. The problem is that my search controller accesses a web service and then updates a UITableView
by calling [tableView reloadData]
. Every time a user enters a key, it searches. I've been able to partially relieve the symptoms by requiring at least three characters before calling the ASIHTTPRequest
but there must be a better way. Basically I want the UISearchDisplayController
to wait for N seconds OR for the user to press "Search" before searching. Here's what I have now:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
if (searchString.length > 2) {
ASIHTTPRequest *request = [IKRequestBuilder requestForIKDogFoodWithQueryString:searchString];
[request setDelegate:self];
[request startAsynchronous];
}
return NO;
}
#pragma mark - ASI HTTP Request
- (void)requestStarted:(ASIHTTPRequest *)request {
//show activity view over table view
}
- (void)requestFinished:(ASIHTTPRequest *)request {
NSString *response = [request responseString];
self.dogFoods = [IKJsonFactory buildIKDogFoodArrayWithJSON:response];
[self.searchDisplayController.searchResultsTableView reloadData];
}
推荐答案
将NSTimer作为属性添加到搜索委托中.
Add an NSTimer as a property on your search delegate.
在shouldReloadTableForSearchString:(NSString *)searchString委托方法中,不要立即发送请求,而是要检查是否已经有一个计时器,如果要取消它,则启动一个新计时器.
In the shouldReloadTableForSearchString:(NSString *)searchString delegate method, instead of sending the request straight away, instead check if you have a timer already, if so cancel it, then start a new timer.
在计时器的目标方法中,开始搜索.
In the timer's target method, kick off the search.
这意味着在他们键入内容时,您将继续创建计时器.当他们停止键入时,计时器的最后一个角色将在n秒内触发.然后,您只需要在他们按下按钮时添加手动开始的搜索即可. (您还可以添加一个布尔值,以检查搜索是否已经开始).
This will mean that while they are typing you will keep recreating the timer. When they stop typing the last incarnation of the timer will fire in n seconds. Then you just need to also add the manual kicking off of the search when they press the button. (you could also add a boolean to check if the search has already been started).
即在您的代表界面中
NSTimer *searchTimer;
@property (nonatomic, retain) NSTimer *searchTimer;
然后在您的shouldReloadTableForSearchString的实现中,而不是触发Web请求,请执行以下操作:
Then in your implementation of the shouldReloadTableForSearchString, instead of firing off the web request, do this:
if (self.searchTimer) {
[self.searchTimer invalidate];
self.searchTimer = nil;
}
self.searchTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(searchTimerPopped:) userInfo:nil repeats:FALSE];
然后在您的计时器目标中
Then in your timer target
-(void) searchTimerPopped:(NSTimer *)sTimer {
// code to fire off the asynchronous web call to do the search
}
这篇关于UISearchDisplayController-等待N秒钟,或者让用户按"Search"(搜索)在进行搜索之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!