【发布时间】:2012-03-18 08:13:35
【问题描述】:
正如标题所说,在我的代码中,当我在我的应用中搜索时,会以一种简单的方式发生这种情况:
stations = [barcelona, madrid, valencia, badajoz]
当我输入“ba”时,它会显示:
mysearch = [barcelona, badajoz]
我想选择 badajoz,所以程序返回该行是位置 1,而在车站中是位置号 3。
有什么想法吗?
PD:与搜索相关的代码
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
self.searchResults = [self.allItems filteredArrayUsingPredicate:resultPredicate];
}
#pragma mark - UISearchDisplayController delegate methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:[self.searchDisplayController.searchBar text]
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:searchOption]];
return YES;
}
编辑:我使用下面的答案编辑了我的代码 - (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"正在推送..."); NSUInteger 行 = [indexPath 行]; //搜索视图之前的旧代码
//index from the search
id searchResult = [searchResults objectAtIndex:row];
int indexForResult = [ListaEstaciones indexOfObject:searchResult];
//more stuff
编辑: 现在它适用于这种情况:使用包含所有元素的列表,它搜索得很好,当我开始搜索它时,问题是当停止搜索并且我选择所有元素列表中的一行时,它仍然使用 de index在之前的搜索项目列表中,发生这种情况是因为在这段代码中我将搜索布尔变量更改为 YES:
- (void)filterContentForSearchText:(NSString*)searchText
scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
self.searchResults = [self.allItems filteredArrayUsingPredicate:resultPredicate];
searching = YES;
但是当我返回所有值的列表时,我不知道在哪里将搜索值更改为 NO。有什么建议?非常感谢。 编辑 2:通过这个网站解决了 http://engineeringtheworld.wordpress.com/2011/04/11/detecting-when-clear-is-clicked-in-uisearchbar-x-button/
【问题讨论】:
标签: iphone objective-c ios uitableview uisearchdisplaycontroller