你应该阅读一些关于 NSDictionary 的教程。我已经在图书搜索应用程序上实现了这个搜索,其中同时搜索了标题和作者。
// 每本书
NSDictionary * book = NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
title, @"TITLE", author, @"AUTHOR", nil];
[dataSource addObject:book];
之后在搜索方法中您可以根据自己的方便进行更改
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[tableData removeAllObjects];
if(searchText != nil && ![searchText isEqualToString:@""]){
for(NSDictionary * book in dataSource){
NSString * title = [book objectForKey:@"TITLE"];
NSString * author = [book objectForKey:@"AUTHOR"];
NSRange titleRange = [[title lowercaseString] rangeOfString:[searchText lowercaseString]];
NSRange authorRange = [[author lowercaseString] rangeOfString:[searchText lowercaseString]];
if(titleRange.location != NSNotFound || authorRange.location != NSNotFound)
[tableData addObject:book];
}
}
[tableView reloadData];
}