Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ITCreative

macrumors member
Original poster
Aug 4, 2011
46
0
in my project i use search bar it is work fine but it is work 2 or 3 time then don't work !!!!

in interface:

Code:
IBOutlet UITableView *libraryTV;
IBOutlet UISearchBar *searchBar; //search bar


NSMutableArray *booktable;
NSMutableArray *sbook;  
NSMutableArray *rowoflibrary;

in implementation:

Code:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar

{
    // only show the status bar’s cancel button while in edit mode

    searchBar.showsCancelButton = YES;
    searchBar.autocorrectionType = UITextAutocorrectionTypeNo;

    // flush the previous search content
    [booktable removeAllObjects];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar

{

    searchBar.showsCancelButton = NO;

}


- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText

{

    [booktable removeAllObjects];// remove all data that belongs to previous search

    if([searchText isEqualToString:@""]|| searchText==nil){

        [libraryTV reloadData];

        return;

    }

    //NSInteger counter = 0;


    for(NSInteger i=0;i<sbook.count;i++)

    {
        book *bok = (book *)[self.sbook objectAtIndex:i];
        NSString *name =bok.book_title;


        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];

        NSRange r = [name rangeOfString:searchText];

        if(r.location != NSNotFound)

        {

            if(r.location== 0)//that is we are checking only the start of the names.

            {

                [booktable addObject:bok];

            }

        }

        //  counter++;

        [pool release];

    }

    [libraryTV reloadData];

}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar

{


    // if a valid search was entered but the user wanted to cancel, bring back the main list content

    [booktable removeAllObjects];

    [booktable addObjectsFromArray:sbook];

    @try{

        [libraryTV reloadData];

    }

    @catch(NSException *e){

    }

    [searchBar resignFirstResponder];

    searchBar.text = @" ";

}

// called when Search (in our case “Done”) button pressed

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar

{

    [searchBar resignFirstResponder];

}
 

jnoxx

macrumors 65816
Dec 29, 2010
1,343
0
Aartselaar // Antwerp // Belgium
Not sure if your fault lies in this piece of code. I don't see anything wrong, and we don't know how you coded the table view, and that doesn't mean, post all your code, so we can scan the bug, it means, there is a logic reason why it shouldn't work, there allways is in coding.
Double check if you are removing the right arrays, calling from the right array,etc.
do some logs with counts, see where it goes wrong.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.