gpt4 book ai didi

iphone - SearchDisplay Controller 没有显示我的搜索结果

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:43:04 26 4
gpt4 key购买 nike

我正在尝试制作一个像 iPhone 联系人模块一样的联系人 TableView 。我有一个按字母顺序排序的表格 View ,带有 header sections (A-B-C-...)。现在我想搜索这个 tableview。搜索后,我应该只有一个部分包含所有搜索结果。

我遇到的问题是它一直显示“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) 时,它给出我是上下文的正确名称。但是当我将 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];

}

此筛选列表中填满了正确的搜索结果。

编辑 Storyboard截图

enter image description here

编辑搜索委托(delegate)方法

#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;
}

最佳答案

尝试创建一个新的引用 socket 连接到您的联系人 TableView Controller 。

当我在一个 View Controller 中放置两个搜索显示 Controller 时。只有一个搜索显示有效。好的有这个 Outlet 连接。看我的post .

关于iphone - SearchDisplay Controller 没有显示我的搜索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15065301/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com