1
votes

i want to add simple searchbar in the table view..program getting run with no error..but when i try to add text in searchbar program gets terminates...following is my code:-

@interface RootViewController : UITableViewController { NSArray *list;

IBOutlet UISearchBar *searchBar;


NSMutableArray *searchresult;

BOOL isSearchon;

BOOL canSelectRow;

} @property(nonatomic, retain)NSArray *list; @property(nonatomic, retain)IBOutlet UISearchBar *searchBar; -(void)doneSearching:(id)sender;

-(void)searchlist;

@end

//in .m

@implementation RootViewController

@synthesize list,searchBar;

-(void)doneSearching:(id)sender

{ isSearchon=NO;

canSelectRow=YES;

self.tableView.scrollEnabled=YES;

self.navigationItem.rightBarButtonItem=nil;

[searchBar resignFirstResponder];

[self.tableView reloadData];

}

-(void)searchlist

{NSString *searchText = searchBar.text;

[searchresult removeAllObjects];



for(NSString *str in list)
{
    NSRange titleResultsRange=[str rangeOfString:searchText options:NSCaseInsensitiveSearch];


    if (titleResultsRange.length > 0)
        [searchresult addObject:str];
}

}

  • (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar {

    [self searchlist]; }

  • (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {

if([searchText length]>0)

{isSearchon =YES;

canSelectRow=YES;

self.tableView.scrollEnabled=YES;

[self searchlist];}

else{ isSearchon =NO;

canSelectRow=NO;

self.tableView.scrollEnabled=NO;

}

[self.tableView reloadData];

}

  • (void)viewDidLoad { [super viewDidLoad];

    list = [[NSArray alloc]initWithObjects:@"rohan",@"vidhya",@"kavita",@"pushkar",nil];

    self.tableView.tableHeaderView = searchBar;

    searchBar.autocorrectionType=UITextAutocorrectionTypeYes;

    searchresult=[[NSMutableArray alloc]init];

    isSearchon =NO;

    canSelectRow=YES;

    self.navigationItem.title = @"Names";

}

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar

{ isSearchon =YES;

canSelectRow=NO;

self.tableView.scrollEnabled=NO;

self.navigationItem.rightBarButtonItem=[[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneSearching:)]autorelease];

}

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1; }

// Customize the number of rows in the table view. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(isSearchon){ return [searchresult count];}

else{

        return [list count];}

}

// Customize the appearance of table view cells. - (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] autorelease];
}

if(isSearchon)
{
    cell.text=[searchresult objectAtIndex:indexPath.row];}

    else{
        cell.text=[list objectAtIndex:indexPath.row];}

// Configure the cell.

return cell;

}

  • (void)dealloc { [super dealloc]; [searchBar release]; }

@end

1

1 Answers

1
votes

I have edited my code in my question...it works fine nw....AnyOne willing to use searchbar in tableview in Iphones can refer the code.