search图像

我的项目中有TableViewController 。 我想在tableview中使用search栏来search图片。 我在我的应用程序中使用这个代码来做到这一点:

 - (void)viewDidLoad { [super viewDidLoad]; _array1 = @[ @"title1",@"title2",@"title3",@"title4",@"title5",@"title6",@"title7",@"title8",@"title9",@"title10"]; _array2 = @[ @"title1",@"title2",@"title3",@"title4",@"title5",@"title6",@"title7",@"title8",@"title9",@"title10"]; _array3 = @[ @"title1",@"title2",@"title3",@"title4",@"title5",@"title6",@"title7",@"title8",@"title9",@"title10"]; _array18 = [[NSArray alloc ]initWithObjects:@"image1",@"image2",@"image3", nil]; _image = [[NSArray alloc ]initWithObjects:[UIImage imageNamed:@"image1.jpg"],[UIImage imageNamed:@"image2.jpg"],[UIImage imageNamed:@"image3.jpg"], nil]; self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; self.searchController.searchResultsUpdater = self; self.navigationItem.searchController = self.searchController; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (self.searchController.isActive) { return [_searchResults count]; } else if (self.tabBarController.tabBar.selectedItem.tag == 1) { return [_array1 count]; } else if (self.tabBarController.tabBar.selectedItem.tag == 2) { return [_array1 count]; } else if (self.tabBarController.tabBar.selectedItem.tag == 3) { return [_array2 count]; } return 0 ; } - (void)updateSearchResultsForSearchController:(UISearchController *)searchController { NSString *searchString = searchController.searchBar.text; [self searchForText:searchString scope:[[searchController.searchBar scopeButtonTitles] objectAtIndex: [searchController.searchBar selectedScopeButtonIndex]]]; [self.tableView reloadData]; } - (void)searchForText:(NSString*)searchText scope:(NSString*)scope { NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText]; _searchResults = [_array18 filteredArrayUsingPredicate:resultPredicate]; } - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope { [self updateSearchResultsForSearchController:self.searchController]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"Cell1"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } if (self.searchController.isActive) { cell.textLabel.text = [_searchResults objectAtIndex:indexPath.row]; } else if (self.tabBarController.tabBar.selectedItem.tag == 1){ cell.textLabel.text = [_array1 objectAtIndex:indexPath.row]; } else if (self.tabBarController.tabBar.selectedItem.tag == 2){ cell.textLabel.text = [_array2 objectAtIndex:indexPath.row]; } else if (self.tabBarController.tabBar.selectedItem.tag == 3){ cell.textLabel.text = [_array3 objectAtIndex:indexPath.row]; } return cell; } 

但在这个代码中,我只能在单元格中search标题。 如何在我的应用程序中添加search图像?

更新

我尝试在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath使用此代码- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

 if ([[_searchResults objectAtIndex:indexPath.row]isEqualToString:@"title1"]) { cell.imageView.image = [_image objectAtIndex:0]; } if ([[_searchResults objectAtIndex:indexPath.row]isEqualToString:@"title2"]) { cell.imageView.image = [_image objectAtIndex:1]; } if ([[_searchResults objectAtIndex:indexPath.row]isEqualToString:@"title3"]) { cell.imageView.image = [_image objectAtIndex:2]; } 

但如果我有100张或更多的图像,这不是简单的解决scheme。 有没有更简单的解决问题?

您可以设置图像的名称等于目的地inputstring(例如:title1.jpg,title2.jpg …,title100.jpg),之后只需要初始化图像,如果图像存在于光盘上。 示例(伪代码):

 NSString imageName = [userInputtedString stringByAppendingPathExtension:”jpg”]; If([self imageWithNameExist:imageName]) { NSString pathToImage = [self getPathToImageWithName:imageName]; UIImage image = [[UI[enter link description here][1]Image alloc] initWithPath:pathToImage]; <use the image> } 

我不使用imageNamed:因为此函数caching已创build的图像。