2013-12-12 91 views
-2

我已經實現瞭如下搜索功能。我能夠檢索基於搜索字符串的行,但他們是空的,當我點擊空搜索結果單元能夠正確導航每個搜索結果。我使用故事板配置的單元格來顯示錶。我的意思是使用標籤來填充數據和圖像。UISearchcontroller無法正常工作

- (void)viewDidLoad 
{ 

    sqlDatabase = [SQLiteDatabase getDatabaseInstance];   
    filteredItemist=[[NSMutableArray alloc] initWithArray:[sqlDatabase fetchCropListBySoilName:soilName]]; 
    [super viewDidLoad]; 
} 




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 
    // Configure the cell... 

    Crop * crop = [filteredItemist objectAtIndex:[indexPath row]]; 


    NSString* str = [NSString stringWithFormat:@"%@.jpg",crop.cropName.lowercaseString]; 

    UIImageView *cropImageView = (UIImageView *)[cell viewWithTag:100]; 
    cropImageView.image = [UIImage imageNamed:str]; 

    UILabel *cropNameLabel = (UILabel *)[cell viewWithTag:70]; 
    cropNameLabel.text = [crop cropName]; 

    return cell; 

} 


- (void)viewWillAppear:(BOOL)animated 
{ 
    self.navigationItem.title = @"Item List"; 
    filteredItemist = [[NSMutableArray alloc]initWithArray:[sqlDatabase fetchCropListBySoilName:soilName]]; 
    [self.tableView reloadData]; 
    self.navigationController.toolbarHidden = YES; 
    [super viewWillAppear:animated]; 
} 


- (void)filterContentForSearchText:(NSString*)searchText 
{ 
    if([searchText length] == 0) 
    { 
     isFiltered = FALSE; 
     [filteredItemist removeAllObjects]; 
     [filteredItemist addObjectsFromArray:[sqlDatabase fetchCropListBySoilName:soilName]]; 

    } 
    else{ 
     isFiltered = TRUE; 
     [filteredItemist removeAllObjects]; 
     for(Crop *i in [sqlDatabase fetchCropListBySoilName:soilName]){ 
      NSRange stringRange = [[i cropName]rangeOfString:searchText options:NSCaseInsensitiveSearch]; 
      if(stringRange.location !=NSNotFound){ 
       [filteredItemist addObject:i]; 
      } 
     } 

    } 
    [self.tableView reloadData]; 

} 

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [self filterContentForSearchText:searchString]; 

    return YES; 
} 

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 
{ 
    [self.searchBar resignFirstResponder]; 
} 

enter image description here

編輯:

更改代碼後self.tableview:

enter image description here

+0

記錄你的'Crop'對象。 –

+0

這些是NSInteger cropId中的屬性; NSString * cropName; NSInteger farmId; NSInteger Soil_id;並且在搜索結果中,對象具有數據,但是在重新加載表格時,不能從故事板填充標籤。 – user2569524

回答

0

取而代之的是:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

使用這樣的:

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

在單獨的表視圖中顯示的搜索結果。因此,如果您在沒有自我的情況下使用tableView,您可能會引用搜索結果表格視圖。

+0

非常感謝。我現在正在獲取數據。但是單元格大小並不是我在初始視圖中獲得的或我在故事板中定義的小得多的大小,因此它將單個單元格分割爲結果表中的兩個單元格。我已將該圖像添加爲我的Edit帖子。請檢查。謝謝 – user2569524

+0

編輯您的heightForRowAtIndexPath方法以反映高度變化。 –