iPhoneの連絡先モジュールのような連絡先テーブルビューを作成しようとしています。header sections
(ABC-...)でアルファベット順にソートされたテーブルビューがあります。今、私はこれを検索したいtableview
。検索後、すべての検索結果を含むセクションが 1 つだけになるはずです。
私が抱えている問題は、それが表示され続けること"No results"
です。しかし、検索結果を保持する配列を見ると、正しい値が含まれています。
今コードのために。
じぶんのCellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *kCellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (tableView == self.searchDisplayController.searchResultsTableView){
NSLog(@"search tableview");
Contact *contact = [self.filteredListContent objectAtIndex:indexPath.row];
NSString *text = [NSString stringWithFormat:@"%@ %@",contact.name,contact.firstName];
NSLog(@"CellForRowAtIndexPath contact text is %@",text);
cell.textLabel.text = text;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}else{
NSLog(@"standaard tableview");
NSString *alphabet = [firstIndex objectAtIndex:[indexPath section]];
//---get all states beginning with the letter---
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@"SELF.name beginswith[c] %@",alphabet];
NSArray *contacts = [listContent filteredArrayUsingPredicate:predicate];
Contact *contact = [contacts objectAtIndex:indexPath.row];
NSString *text = [NSString stringWithFormat:@"%@ %@",contact.name,contact.firstName];
cell.textLabel.text = text;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
return cell;
}
これが問題です。IF thisself.searchDisplayController.isActive
にいて、 this log を見るとNSLog(@"CellForRowAtIndexPath contact text is %@",text)
、コンテキストの正しい名前が表示されます。しかし、ifをこれに変更すると。tableView == self.searchDisplayController.searchResultsTableView
その後、常に ELSE 部分に移動します。
じぶんのfilterContentForSearchText
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSLog(@"filterContentForSearchText called");
/*
Update the filtered array based on the search text and scope.
*/
[self.filteredListContent removeAllObjects]; // First clear the filtered array.
/*
Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array.
*/
for (Contact *contact in listContent)
{
NSString *searchString = [NSString stringWithFormat:@"%@ %@",contact.name,contact.firstName];
NSRange range = [searchString rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
[self.filteredListContent addObject:contact];
[self.searchDisplayController.searchResultsTableView reloadData];
}
}
NSLog(@"filterd list: %@",[self.filteredListContent valueForKey:@"name"]);
[self.tableView reloadData];
}
このフィルタリングされたリストには、正しい検索結果が表示されます。
編集 ストーリーボードのスクリーンショット
検索デリゲート メソッドの編集
#pragma mark - UISearchDisplayController Delegate Methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
// Tells the table data source to reload when text changes
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
// Tells the table data source to reload when scope bar selection changes
[self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}