user3322987
user3322987

Reputation: 63

Search Display Controller display additional tableView

Why another table view displayed after I inserted some word inside the search bar?

Before writing in search box

enter image description here

After writing in search box

enter image description here

Here is the code I am using:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [NSString stringWithFormat:@"%@", filteredFields[indexPath.row]];
    }
    else
    {
        cell.textLabel.text = [NSString stringWithFormat:@"%@", oriFields[indexPath.row]    ];
    }


    return cell;
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];
    filteredFields = (NSMutableArray *)[oriFields filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}

Upvotes: 1

Views: 647

Answers (2)

Armand DOHM
Armand DOHM

Reputation: 1131

you probably use a NSMutableArray as your data source... And you don't clean it when you type your search, so you have twice the values in it... Isn't it ?

Upvotes: 0

codercat
codercat

Reputation: 23291

A search display controller manages the display of a search bar, along with a table view that displays search results.

You initialize a search display controller with a search bar and a view controller responsible for managing the data to be searched. When the user starts a search, the search display controller superimposes the search interface over the original view controller’s view and shows the search results in its table view.

referenced by apple

Upvotes: 1

Related Questions