我有一个应用程序,我正在使用带有 tableview 的搜索栏。当在搜索栏中输入特定单词并单击搜索按钮时,与该特定关键字匹配的所有值都显示在 tableview 中。但是当我单击搜索栏时再次开始删除特定单词,然后我的应用程序崩溃。这是我用于搜索文本并在表格视图中显示的代码。
-(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;
}
}