我有两个数组:
NSMutableArray *moviesArray;
NSMutableArray *directorsArray;
moviesArray = [@"movie01",@"movie02",@"movie03",@"movie04"];
directorsArray = [@"director01",@"director02",@"director03",@"director04"];
我在表格视图中显示了这一点。每个Cell都有一个Title Label和一个Director Label。
cell.titleLabel.text = [moviesArray objectAtIndex:indexPath.row];
cell.directorLabel.text = [directorsarray objectatIndex:indexPath.row];
我正在使用搜索栏来搜索电影片名,所以我这样做:
//Search Filter Code
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];
searchResults = [moviesArray filteredArrayUsingPredicate:resultPredicate];
NSLog(@"Search Results: %@", searchResults);
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
然后在我的手机中:
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.pdmTitle.text = [searchResults objectAtIndex:indexPath.row];
[...] }
这一切都有效。我的问题是,每部电影都有相应的导演导演。 如何在我的单元格中显示匹配的导演?
意思是,如果我搜索电影03,titleLabel.text应该是movie03,directorLabel.text应该是director03。
非常感谢你的帮助!
答案 0 :(得分:3)
您是否考虑使用单个NSDictionary
而不是使用两个NSArrays
?
例如:
NSDictionary *movies=@{@"movie1":@"director1",@"movie2":@"director2"};
通过以下方式,您可以随时了解电影导演:
[movies objectForKey:@"movie1"];
如果您想存储有关每部电影的更多信息,那么最好的方法是使用array
dictionaries
,例如:
NSDictionary *movie=@{@"MovieName":@"Norbit",@"Director":@"Eddie Murphy",@"Language":@"English"};
NSArray *movies=@[movie];
答案 1 :(得分:0)
NSString *correspondingDirector = [directorsArray objectAtIndex: [moviesArray indexOfObject:@"Movie you are searching"]];
答案 2 :(得分:0)
根据您的代码添加:
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.pdmTitle.text = [searchResults objectAtIndex:indexPath.row];
// line to add:
NSString *director = [directorsArray objectAtIndex:[moviesArray indexOfObject:cell.pdmTitle.text]];
[...] }