0

I am learning to use Search Bars a Search Display Controllers in iOS. I made a simple test project with two views, one view contains a UITableView and a UISearchBar, when you select an row from the table view, the new view shows a label with the name of the row. I'm getting a couple of problems, first when I start to add text in the search bar and the search display appears, the search bar is gone. The filtered results are shown, but when I select a row it does not take me to the next view, this only happens when the table is filtered. Here is the code:

ViewController.h

#import <UIKit/UIKit.h>
#import "DetailViewController.h"

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchDisplayDelegate, UISearchBarDelegate>

@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic) IBOutlet UITableView *tableView;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) NSArray *objects;
@property (nonatomic, strong) NSArray *filteredObjects;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    _objects = [[NSMutableArray alloc] initWithObjects:@"One", @"Two", @"Three", @"Four", @"Five", @"Six", @"Seven", @"Eight", @"Nine", @"Ten", nil];
    _tableView.delegate = self;
    _tableView.dataSource = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [_filteredObjects count];

    } else {
        return [_objects count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

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

    return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"ToDetail"]) {
        DetailViewController *detailViewController = [segue destinationViewController];

        if (self.searchDisplayController.active) {
            NSIndexPath *indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            detailViewController.detailString = [_filteredObjects objectAtIndex:indexPath.row];
        } else {
            NSIndexPath *indexPath = [_tableView indexPathForSelectedRow];
            detailViewController.detailString = [_objects objectAtIndex:indexPath.row];
        }

    }
}

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

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

    return YES;
}

Thanks for the help!

rene
  • 7
  • 1
  • add delegate method didSelectRowAtIndexPath and check if the table view is the searchDisplayController's tableview and then call method [self performSegueWithIdentifier:sender:. The problem might be that you have selection segue in the storyboard but the viewcontroller takes is as the tableviews segue not the segue when you click the tableview in searchDisplayController. – Sandeep Feb 23 '14 at 22:09

1 Answers1

0

when I select a row it does not take me to the next view, this only happens when the table is filtered

Because you have no implementation of tableView:didSelectRowAtIndexPath:.

Remember, there is really no such thing as a "filtered table view". There are two table views: the normal one, and the one that appears because of the search display controller. That is a different table view (basically appearing in front of yours), and you must configure it for whatever you want it to do and however you want it to look.

matt
  • 515,959
  • 87
  • 875
  • 1,141