다음과 같이 검색 기능을 구현했습니다. 검색 문자열을 기반으로 행을 검색 할 수 있지만 비어 있습니다. 빈 검색 결과 셀을 클릭하면 각 검색 결과에 대해 올바르게 탐색 할 수 있습니다. 스토리 보드 구성 셀을 사용하여 테이블을 표시합니다. 데이터와 이미지를 채우기 위해 태그를 사용하고 있습니다.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];
}
편집 :
코드를 변경 한 후이 self.tableview하는 대신이의
'Crop' 개체를 기록하십시오. –
이들은 NSInteger cropId의 속성입니다. NSString * cropName; NSInteger farmId; NSInteger Soil_id; 검색 결과에서 객체에는 데이터가 있지만 테이블 재로드시 스토리 보드에서 Lable을 채울 수 없습니다. – user2569524