Search Bar with Table View...
Storyboard...
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (weak, nonatomic) IBOutlet UITableView *searchTableView;
@end
#import "ViewController.h"
@interface ViewController ()
{
NSMutableArray *contactsArray;
NSMutableArray *filteredContactsArray;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
contactsArray = [[NSMutableArray alloc]initWithObjects:@"Arun",@"Arjun",@"Amar",@"Abhiash",@"abc",@"bcd",@"def",@"fgh",@"hij",@"jkl",@"lmn",@"nop",@"pqr",@"rst",@"tuv",@"vuw",@"wxy",@"yza",@"bca",@"cad",@"hello",@"thrymr",@"apple",@"kapil",@"tower",@"nanakram",@"icici",@"google",@"mac",@"ios",@"prgm",@"window",@"Xcode",@"simulator",@"bundle",@"section",@"row", @"Arun2", @"Ravi", @"ravi2", @"Krishna",@"Krishna2",@"Srikanth",@"Srikanth2",@"Arun12", @"Arun2gfdf", @"Ravidfg", @"ravi2sdas", @"Krishna",@"Krishna2",@"Srikanth",@"Srikanth2",@"Arun32r", @"Arun2fdf", @"Raviasdaf", @"ravi2", @"Krishna",@"Krishna2",@"Srikanth", nil];
filteredContactsArray = [[NSMutableArray alloc] init];
// self.searchBar.showsCancelButton = YES;
// self.searchBar.showsSearchResultsButton =YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - searchbar delegate and custom methods
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
// You can write search code Here
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
return YES;
}
- (void)filterContentForSearchText:(NSString*)searchText
{
@try {
NSString *newString= [searchText stringByReplacingOccurrencesOfString: @"[ \t]+_"
withString: @" "
options: NSRegularExpressionSearch
range: NSMakeRange(0, searchText.length)];
NSPredicate *subpredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", newString];
[filteredContactsArray setArray:[contactsArray filteredArrayUsingPredicate:subpredicate]];
NSLog(@"Results Array : %@",filteredContactsArray);
if ([filteredContactsArray count] == 0)
{
[self.searchTableView setHidden:YES];
}
else if([filteredContactsArray count] == 1)
{
self.searchTableView.hidden = NO;
}
else
{
[self.searchTableView setHidden:NO];
}
[self.searchTableView reloadData];
}
@catch (NSException *exception) {
}
@finally {
}
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
@try {
if (searchText.length == 0)
{
[filteredContactsArray removeAllObjects];
[self.searchTableView reloadData];
[self.searchTableView setHidden:YES];
}
else
{
[self filterContentForSearchText:searchText];
}
}
@catch (NSException *exception) {
}
@finally {
}
}
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
[self.searchTableView setHidden:YES];
}
- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar
{
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return filteredContactsArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"CellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier] ;
}
@try {
cell.textLabel.text = [filteredContactsArray objectAtIndex:indexPath.row];
}
@catch (NSException *exception) {
}
@finally {
}
return cell;
}
@end
Result...
No comments:
Post a Comment