searchdisplaycontroller: change the text of the searchbar

Posted by kudorgyozo on Stack Overflow See other posts from Stack Overflow or by kudorgyozo
Published on 2010-03-04T10:12:30Z Indexed on 2010/03/15 8:59 UTC
Read the original article Hit count: 462

Hello,

SHORT DESCRIPTION OF PROBLEM: I want to set the text of a searchbar without automatically triggering the search display controller that is bound to it.

LONG DESCRIPTION OF PROBLEM: I have an iphone application with a search bar and a search display controller. The searchdisplaycontroller is used for autocomplete. For autocomplete i use an sqlite database. The user enters the first few letters of a keyword and the result are shown in the table of the searchdisplaycontroller. An sql select query is executed for every character typed. this part works ok, the letters have been entered and the results are visible.

The problem is the following: If the user selects a row from the table I want to change the text of the searchbar to the text that was selected in the autocomplete results table. I also want to hide the search display controller. This is not working. After the search display controller disappears the textbox in the search bar is empty. I have no idea what's wrong. I didn't think something so simple as changing the text of a textbox can get so complicated.

I have tried to change the text in 2 methods: First in the didSelectRowAtIndexPath method (for the search results table of the search display controller), but that didn't help. The text was there while the search display controller was active (animating away) but after that the textbox was empty.

 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"didSelectRowAtIndexPath");

    if (allKeywords.count > 0)
    {
        didSelectKeyword = TRUE;
        self.selectedKeyword = (NSString *)[allKeywords objectAtIndex: indexPath.row];
        NSLog(@"keyword didselectrow at idxp: %@", self.selectedKeyword);

        shouldReloadResults = FALSE;

        [[self.mainViewController keywordSearchBar] setText: selectedKeyword];

        //shouldReloadResults = TRUE;

        [[mainViewController searchDisplayController] setActive:NO animated: YES];
    }
}

I also tried to change it in the searchDisplayControllerDidEndSearch method but that didn't help either. The textbox was...again.. empty.

edit: actually it wasnt empty the text was there but after the disappearing animation the results of the autocomplete table are still there. So it ends up getting worse.


- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
    NSLog(@"searchDisplayControllerDidEndSearch");
    if (didSelectKeyword)
    {
        shouldReloadResults = FALSE;
        NSLog(@"keyword sdc didendsearch: %@", selectedKeyword);

        [[mainViewController keywordSearchBar] setText: selectedKeyword]; //

In this method i call another method which selects the data from sqlite. This part is working.


- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    NSLog(@"shouldReloadResults : %i", shouldReloadResults);
    if (shouldReloadResults)
    {
        NSLog(@"shouldReloadTableForSearchString: %@", searchString);
        [self GetKeywords: searchString : @"GB"];
        NSLog(@"shouldReloadTableForSearchString vege");
        return YES;
    }
    return NO;
}

Please ask me if it's not clear what my problem is. I need your help. Thank you

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about uisearchdisplaycontroller