0

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;

        }
}
Rani
  • 3,333
  • 13
  • 48
  • 89
  • try zombies http://stackoverflow.com/questions/5386160/how-to-enable-nszombie-in-xcode – nielsbot Aug 23 '12 at 07:20
  • @ShivanRaptor it throws this error [NSCFString objectForKey:]: unrecognized selector sent to instance 0x5f404f0 2012-08-23 12:59:49.028 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x5f404f0' at the line that i have mentioned in the question – Rani Aug 23 '12 at 07:30
  • @nielsbot it throws this error [NSCFString objectForKey:]: unrecognized selector sent to instance 0x5f404f0 2012-08-23 12:59:49.028 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x5f404f0' – Rani Aug 23 '12 at 07:30
  • Looks like you are adding NSString instances to 'searcharray' and trying to read 'NSDictionary' from it in cellForRowAtIndexPath method. You are passing objectForKey: message/selector to NSString which is causing the app to crash as for NSString it is an unrecognized selector. – Amar Aug 23 '12 at 07:52

0 Answers0