Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Implement Search Bar in iOS 7. Searching works fine but the resulting content loaded is not corresponds to the search result

How to select the search result of table view instead of loading normal table view contents

In my iOS project using UISearchBar. Searching works fine for me but in the time of selecting the search resulting row the did select row methods loading original items instead of the search resulting row. What method should I use for load the resultant row and its corresponding detail page.

Thanks,

code:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {    // Tells the table data source to reload when text changes
     [self filterContentForSearchText:searchString scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:      
     [self.searchDisplayController.searchBar selectedScopeButtonIndex]]];    

     // Return YES to cause the search result table view to be reloaded.
     return YES;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     [self performSegueWithIdentifier:@"SubCategory" sender:self];
}      

#pragma mark Content Filtering
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
    // Update the filtered array based on the search text and scope.
    // Remove all objects from the filtered search array
    //[self.filteredArray removeAllObjects];
    // Filter the array using NSPredicate
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",searchText];
    filteredArray=[categoryNameArray filteredArrayUsingPredicate:predicate];     
}        

#pragma mark - UISearchDisplayController Delegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    // Tells the table data source to reload when text changes
    [self filterContentForSearchText:searchString scope:
    [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:      
    [self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

    // Return YES to cause the search result table view to be reloaded.
    return YES;
}    

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUserDefaults *usr=[NSUserDefaults standardUserDefaults];
    NSString*fontc=[usr stringForKey:@"fontchange"];
    float fontSize=[fontc floatValue];
    newfont= [UIFont fontWithName:@"Helvetica" size:fontSize];
    static NSString *simpleTableIdentifier = @"SimpleTableCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

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


    if (tableView==self.searchDisplayController.searchResultsTableView)
    {
       cell.textLabel.text=[filteredArray objectAtIndex:indexPath.row];
    } else {
       cell.textLabel.text=[categoryNameArray objectAtIndex:indexPath.row];
    }
    cell.textLabel.font=newfont;
    UIImage *img;

    img = [UIImage imageNamed:@"icon_generic.png"];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    cell.imageView.image=img;
    return cell;
}      
like image 910
SARATH SASI Avatar asked Jan 28 '15 10:01

SARATH SASI


1 Answers

    - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    // Tells the table data source to reload when text changes

     [self filterContentForSearchText:searchString scope:

     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:

      [self.searchDisplayController.searchBar selectedScopeButtonIndex]]];    

    // Return YES to cause the search result table view to be reloaded.
    return YES;}
     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    [self performSegueWithIdentifier:@"SubCategory" sender:self]; 
}   
#pragma mark Content Filtering
    - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
    // Update the filtered array based on the search text and scope.

    // Remove all objects from the filtered search array
    //[self.filteredArray removeAllObjects];

    // Filter the array using NSPredicate
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",searchText];

    filteredArray=[categoryNameArray filteredArrayUsingPredicate:predicate]; }        
#pragma mark - UISearchDisplayController Delegate Methods
    - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    // Tells the table data source to reload when text changes

    [self filterContentForSearchText:searchString scope:

     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:

      [self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

    // Return YES to cause the search result table view to be reloaded.
    return YES;
}
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSUserDefaults *usr=[NSUserDefaults standardUserDefaults];
    NSString*fontc=[usr stringForKey:@"fontchange"];
    float fontSize=[fontc floatValue];
    newfont= [UIFont fontWithName:@"Helvetica" size:fontSize];
    static NSString *simpleTableIdentifier = @"SimpleTableCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

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


    if (tableView==self.searchDisplayController.searchResultsTableView)
    {
       cell.textLabel.text=[filteredArray objectAtIndex:indexPath.row];

    } else

    {
        cell.textLabel.text=[categoryNameArray objectAtIndex:indexPath.row];

    }
    cell.textLabel.font=newfont;
    UIImage *img;

    img = [UIImage imageNamed:@"icon_generic.png"];

    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

    cell.imageView.image=img;

    return cell;}      
like image 161
SARATH SASI Avatar answered Nov 14 '22 08:11

SARATH SASI