我已经在表格视图中实现了搜索栏和搜索文本。我遇到的问题是,当我滚动表格视图然后突然单击搜索栏会导致应用程序退出。
有任何想法,当点击搜索栏时,我们如何做任何事情来停止滚动。
我的代码如下:
- (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];
}
}