Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use search bar in UITableView

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 258
    Comment on it

    This is very helpful, if you want to use search functionality on UITableView.

    Code for ViewController.h

    @interface  ViewController : UIViewController<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate,UISearchDisplayDelegate>{
    
        IBOutlet UISearchBar *telepathySearch;
        IBOutlet UITableView *telepathyTable;
    
    }
    
    @property (nonatomic, weak) NSMutableArray *searchResults;
    @property (nonatomic, weak) NSMutableArray *tableData;
    

    Code for ViewController.m

    - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
    {
        [self.searchResults removeAllObjects];
    
        NSPredicate *resultPredicate = [NSPredicate
                                        predicateWithFormat:@"SELF contains[cd] %@",
                                        searchText];
    
        self.searchResults = [NSMutableArray arrayWithArray:[self.tableData filteredArrayUsingPredicate:resultPredicate]];
    }
    
    -(BOOL)searchDisplayController:(UISearchDisplayController *)controller
    shouldReloadTableForSearchString:(NSString *)searchString
    {
        [self filterContentForSearchText:searchString
                                   scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                          objectAtIndex:[self.searchDisplayController.searchBar
                                                         selectedScopeButtonIndex]]];
    
        return YES;
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: