テーブルビューで検索バーを使用しているアプリケーションがあります。特定の単語を検索バーに入力して検索ボタンをクリックすると、その特定のキーワードに一致するすべての値がテーブルビューに表示されます。しかし、検索バーをクリックするともう一度特定の単語の削除を開始すると、アプリがクラッシュします。これは、テキストを検索してテーブルビューに表示するための私のコードです。
-(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];
}
セルに表示している間、私は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;
}
}