我正在尝试使联系人表视图像iPhone通讯录模块。我有一个按字母顺序排序的header sections
(A-B-C -...)。现在我想搜索这个tableview
。我的搜索后,我应该只有一个部分的所有搜索结果。SearchDisplay控制器不显示我的搜索结果
我现在的问题是它一直显示"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这个self.searchDisplayController.isActive
,我看着这个日志NSLog(@"CellForRowAtIndexPath contact text is %@",text)
,那么它给了我正确的上下文名称。但是当我改变如果这个。 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;
}
你使用searchbar或textField作为fillter? – iPatel 2013-02-25 10:55:38
什么是self.searchDisplayController.searchResultsTableView,你确定它不是零? – borrrden 2013-02-25 11:02:06
@iPatel我想我使用textfield。 – Steaphann 2013-02-25 11:04:50