Sunday, January 08, 2012

iOS: search bar with table view

// assuming table view is done
VC .h

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate>
{
    // ADDED
    NSMutableArray *tableData;
    NSArray *array;
    NSMutableArray *searchResults;
    
    IBOutlet UITableView *tableView;
}

@property (nonatomic, retain) NSArray *array;

@end


// assuming table view is done
VC .m
// ADDED: to search
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSLog(@"searching: %@", searchText);
    searchText = [searchText lowercaseString];
    // search for text in array
    if(searchText.length > 0)
    {
        [tableData removeAllObjects];// clear all data
        [searchResults removeAllObjects];
        for (NSString *ostr in array)
        {
            //NSLog(@"looping: %@", str);
            NSString *str = [ostr lowercaseString];
            NSRange range = [str rangeOfString:searchText];
            if(range.location != NSNotFound)
            {
                NSLog(@"matched: %@", searchText);
                [searchResults addObject:ostr];
            }
        }
        //[tableData removeAllObjects];
        [tableData addObjectsFromArray:searchResults];
        [tableView reloadData];
    }
    else
    {
        [tableData removeAllObjects];// clear all data
        [tableData addObjectsFromArray:array];
        [tableView reloadData];
    }
}


- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"Cancel Button Clicked");
    [searchBar resignFirstResponder]; // close keyboard
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"Search Button Clicked");
    [searchBar resignFirstResponder]; // close keyboard

}

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    tableView.delegate = self;
    tableView.dataSource = self;
    
    // ADDED
    self.array = [NSArray arrayWithObjects:@"Beng", @"Seng", @"Lian", @"Beng2", @"Seng2", @"Lian2", nil];
    // must alloc and init. strange. cannot use arrayWithArray
    tableData = [[NSMutableArray alloc] init]; //[NSMutableArray arrayWithArray:self.array];
    searchResults = [[NSMutableArray alloc] init];//[NSMutableArray arrayWithArray:self.array];
    [tableData addObjectsFromArray:self.array];
    
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width0)];
    [searchBar sizeToFit];
    searchBar.delegate = self;
    [searchBar setShowsCancelButton:YES animated:YES];
    //[searchBar showsSearchResultsButton];
    [self.view addSubview:searchBar];
    
    // place tableview just below search bar. else, the first row of the table view will be hidden behind search bar
    // hence adjust the y and height in the (x,y,w,h) of the table view's frame
    [tableView setFrame:CGRectMake(0, searchBar.frame.size.height, tableView.frame.size.width, tableView.frame.size.height - searchBar.frame.size.height)];
}



1 comment:

Giancarlo Leonio said...

Hi thank you for this post. I found it helpful in implementing an iOS searchbar in tableview. I compiled a list of top resources I found on this topic. Hope other developers find it useful too. Check it out/ feel free to share.
http://www.verious.com/board/Giancarlo-Leonio/implementing-an-ios-search-bar-in-table-view/