UISearchDisplayController - wait for N seconds OR for user to press “Search” before conducting search

点点圈 提交于 2019-12-08 01:20:22

问题


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];
}

回答1:


Add an NSTimer as a property on your search delegate.

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.

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

i.e. in your delegates interface

NSTimer *searchTimer;

@property (nonatomic, retain) NSTimer *searchTimer;

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
}


来源:https://stackoverflow.com/questions/9347369/uisearchdisplaycontroller-wait-for-n-seconds-or-for-user-to-press-search-bef

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