テーブルビューで検索バーと検索テキストを実装しました。私が得ている問題は、テーブルビューをスクロールしてから突然検索バーをクリックすると、アプリが終了することです。
検索バーをクリックしたときにスクロールを停止する方法を考えてみてください。
私のコードは次のとおりです。
- (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];
}
}