2013-12-12 3 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; 검색 결과에서 객체에는 데이터가 있지만 테이블 재로드시 스토리 보드에서 Lable을 채울 수 없습니다. – user2569524

답변

0

:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

사용이 : 별도의 테이블보기에 표시

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

검색 결과. 그러므로 자기없이 tableView을 사용하면 검색 결과 테이블보기를 참조하는 것일 수 있습니다.

+0

고마워요. 나는 지금 자료를 얻고있다. 하지만 셀 크기는 내가 처음보기에서 얻은 것이거나 스토리 보드에서 정의한 것보다 훨씬 작아서 결과 테이블에서 두 개의 셀로 단일 셀을 분할하는 것입니다. 이미지를 편집에 추가했습니다. 게시하다 . Pls 수표. 고맙습니다. – user2569524

+0

heightForRowAtIndexPath 메소드를 편집하여 높이 변경을 반영하십시오. –