我遇到了一些搜索代码的问题。我有一个名为searchedData
的NSMutableArray,它包含NSDictionaries(每个对象一个)。下面是搜索代码我现在有:NSArray中的NSDictionary搜索
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[tableData removeAllObjects]; // remove all data that belongs to previous search
if([searchText isEqualToString:@""]||searchText==nil) {
[tableView reloadData];
return;
}
int i = 0;
while (i < [dataSource count]) {
NSDictionary *coolDict = [searchedData objectAtIndex:i];
NSString * title = [coolDict objectForKey:@"TITLE"];
NSString * authorString = [coolDict objectForKey:@"AUTHOR"];
NSRange titleRange = [[title lowercaseString] rangeOfString:[searchText lowercaseString]];
NSRange authorRange = [[authorString lowercaseString] rangeOfString:[searchText lowercaseString]];
if (titleRange.location != NSNotFound || authorRange.location != NSNotFound)
[tableData addObject:title];
i++;
}
[tableView reloadData];
}
它发现条目的数量,然后去每个条目,发现钥匙“标题”和“作者”的对象,然后在UITableView中显示的条目,如果有是一场比赛。
问题是即使有匹配,它也不会显示任何东西。 我知道表/字典不是空(我有NSLogged它),所以我不知道为什么它不工作。
为什么你的'while'循环为每个'datasource'循环,而是从'searchedData'中取值?这是一个错字还是两个相互镜像? – 2012-02-17 03:51:07
它们不互相镜像,但它们包含相同数量的对象。 – 2012-02-17 13:09:40
顺便说一句,如果一个事件发生时作者和书的标题是相同的,你会有点问题 - 在你的-tableView:didSelectRow:方法 – 2012-02-17 13:28:52