i have an application where i am using a searchbar with a tableview.When a particular word is entered in the searchbar and clicked on the search button all the values matching that particular keyword are displayed in the tableview.But when i click on the search bar again and start deleting a particular word,then my app crashes.This is my code for searching text and displaying in tableview.
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
if ([searcharray count]>0)
{
canSelectRow = YES;
self.tblView.scrollEnabled = YES;
}else {
canSelectRow = NO;
self.tblView.scrollEnabled = NO;
}
isSearchOn = YES;
searchBar.showsCancelButton = YES;
[searchBar setShowsCancelButton:YES animated:YES];
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
}
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)searchText
{
[searcharray removeAllObjects];
if ([searchText length]>0)
{
isSearchOn = YES;
canSelectRow = YES;
self.tblView.scrollEnabled = YES;
[self searchTableView];
}
else {
isSearchOn = NO;
canSelectRow = NO;
self.tblView.scrollEnabled = NO;
}
[self.tblView reloadData];
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
searchBar.showsCancelButton = NO;
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
searchBar.text = @"";
testSearch = NO;
canSelectRow = YES;
self.tblView.scrollEnabled = YES;
[searchBar resignFirstResponder];
[self.tblView reloadData];
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar1
{
testSearch = TRUE;
NSString *searchtext = searchBar.text;
int tag =(NSNumber*) [[NSUserDefaults standardUserDefaults] integerForKey:@"buttontag"];
if (tag==0)
{
//calling my api method to display on the tableview.
}
else if(tag==1)
{
//calling my api method to display on the tableview.
}
else if(tag==2)
{
//calling my api method to display on the tableview.
}
else if(tag==3)
{
//calling my api method to display on the tableview.
}
else if(tag==4)
{
//calling my api method to display on the tableview.
}
else if(tag ==5)
{
//calling my api method to display on the tableview.
}
[searchBar resignFirstResponder];
}
and while displaying in the cell i am using the searcharray
tableview cellForRowAtIndexPath
{
//testSearch is the bool variable that i have returned true when search button is clicked
if (testSearch)
{
//through the search arrray i am displaying in cell.
NSDictionary *dict = [searcharray objectAtIndex:indexPath.row];
// app is crashing at this point.
****cell.lblPassion.text = [dict objectForKey:@"Title"];****
cell.lblCost.text = [dict objectForKey:@"DescriptionMobile"];
NSString *startdate =[dict objectForKey:@"StartDtm"];
NSDate *newstartdate = [dateformatter dateFromString:startdate];
NSString *custommessage = (NSString*)[dict objectForKey:@"OfferCustomMessage"];
if ( custommessage !=nil && custommessage !=NULL && custommessage !=(NSString*) [NSNull null]) {
cell.lblCustomMsg.hidden = NO;
cell.lblCustomMsg.text = custommessage;
cell.lblDiscount.hidden = YES;
cell.lblDeal.hidden = YES;
cell.lblStrikeout.hidden = YES;
cell.strikeimage.hidden = YES;
}
}