我遇到了一些搜索代碼的問題。我有一個名爲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