0
votes

I'm trying to add a UISearchBar to a UITableView but the method TextDidBeginEditing is not being called. I think I am missing something simple but I can't figure it out.

Header File:

@interface TableViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>

@property (nonatomic, strong) NSMutableArray *initialCities;
@property (nonatomic, strong) NSMutableArray *filteredCities;
@property (nonatomic, strong) UISearchBar *searchBar;
@property BOOL isFiltered;

@end

Implementation File:

#import "TableViewController.h"

@interface TableViewController ()

@end

@implementation TableViewController

@synthesize initialCities, filteredCities, isFiltered, searchBar;

- (void)loadView
{
    [super loadView];
    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44.0)];
    searchBar.delegate = self;
    searchBar.autoresizingMask = (UIViewAutoresizingFlexibleWidth);
    searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
    self.tableView.tableHeaderView = searchBar;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.tableView.delegate = self;

    initialCities = [[NSMutableArray alloc] initWithObjects:@"London", @"New York", @"Berlin", nil];
}

#pragma mark - UITableView DataSource Methods

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (isFiltered == YES)
    {
        return filteredCities.count;
    }
    else
    {
        return initialCities.count;
    }
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

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

    if (isFiltered == YES)
    {
        cell.textLabel.text = [filteredCities objectAtIndex:indexPath.row];
    }
    else
    {
        cell.textLabel.text = [initialCities objectAtIndex:indexPath.row];
    }

    return cell;
}

#pragma mark - UITableView Delegate Methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}

#pragma mark - UISearchBar Delegate methods

- (void)searchBar:(UISearchBar *)searchBar TextDidBeginEditing:(NSString *)searchText
{
    searchBar.showsCancelButton = YES;

    if (searchText.length == 0)
    {
        isFiltered = NO;
    }
    else
    {
        isFiltered = YES;

        filteredCities = [[NSMutableArray alloc] init];

        for (NSString *cityName in initialCities)
        {
            NSRange cityNameRange = [cityName rangeOfString:searchText options:NSCaseInsensitiveSearch];

            if (cityNameRange.location != NSNotFound)
            {
                [filteredCities addObject:cityName];
            }
        }
    }

    [self.tableView reloadData];

}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

The result is that the search bar is displayed, the table view is displayed, but when I type text into the search bar the table view is not filtered. There is no crash. Any help is appreciated. Thank you.

3
Hopefully you are using ARC...Faisal Ali

3 Answers

1
votes

Try this

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)searchText
{    
    // Your filter code
}

The method you implemented is not even present in Protocol

– searchBar:textDidChange:
– searchBar:shouldChangeTextInRange:replacementText:
– searchBarShouldBeginEditing:
– searchBarTextDidBeginEditing:
– searchBarShouldEndEditing:
– searchBarTextDidEndEditing:
0
votes
  • (void)searchBar:(UISearchBar *)searchBar TextDidBeginEditing:(NSString *)searchText

This method is not existed in searchBar's delegate.

Use searchBarTextDidBeginEditing.

0
votes

Go through this tutorial. well explained. http://www.appcoda.com/search-bar-tutorial-ios7/

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