I have implemented searchbar and searching text in table view . The problem I m getting is that when I scrolls the table view and then suddenly click on search bar causes app to quit.
have any body idea how can we do anything to stop scroll when click on search bar.
My code is as follows:
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
NSLog(@"becomes first responder");
[table setScrollEnabled:NO];
return YES;
}// return NO to not become first responder
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
isSearchOn=YES;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
searchBar.text=@"";
isSearchOn=NO;
[searchBar resignFirstResponder];
[table setScrollEnabled:YES];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if ([searchText length]>0)
isSearchOn=YES;
else
isSearchOn=NO;
NSLog(@"search text %@",searchText);
[self searchText:searchText];
[table reloadData];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
NSLog(@"end editing called.....");
searchBar.text=@"";
[table setScrollEnabled:YES];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
-(void)searchText:(NSString*)str
{
NSLog(@"search text");
[resultArray removeAllObjects];
if (segmentControl.selectedSegmentIndex==0) {
for (NSDictionary *CellDix in FriendSuggestion)
{
NSRange titleResultRange = [[CellDix valueForKey:@"itemid.item"] rangeOfString:str options: (NSCaseInsensitiveSearch | NSLiteralSearch)];
NSRange titleRangePopular=[[CellDix valueForKey:@"usagecategoryid.usagecategory"] rangeOfString:str options:(NSCaseInsensitiveSearch | NSLiteralSearch)];
if (titleResultRange.length > 0 || titleRangePopular.length>0)
[resultArray addObject:CellDix];
}
}else
{
for (NSDictionary *CellDix in popularSuggestion)
{
NSRange titleResultRange = [[CellDix valueForKey:@"itemid.item"] rangeOfString:str options: (NSCaseInsensitiveSearch | NSLiteralSearch)];
NSRange titleRangePopular=[[CellDix valueForKey:@"usagecategoryid.usagecategory"] rangeOfString:str options:(NSCaseInsensitiveSearch | NSLiteralSearch)];
if (titleResultRange.length > 0 || titleRangePopular.length>0)
[resultArray addObject:CellDix];
}
}
Clicking on search bar will on isSearch bool variable and as U would have applied condition in cellforRowAtIndexpath will call and it checks for isSearch on then it works accordingly and result array will be empty there because U have not written any thing on search bar that is to be checked and load and resultArray with values . That’s why it is quiting the app because it founds null array in cellforRowAtIndexpath to fill cell values.So better one is that u have to on isSearch on textDidChange in searchbar not in searchBarTextDidBeginEditing delegate method . So comment this method.