I have set up a search bar for my table view, and I would like to have it search through both the textLabels (main text), and detailTextLabels (underlying text), while maintaining the relationship between the main labels and detail labels when narrowing down the search. Right now, I can search through the main labels only, but the detail labels will not stay with the related main labels. I have two arrays that carry the strings for the main labels and the detail labels. Here is some code.
The initialization of the arrays carrying the main labels (groceryStores) and the detail labels (groceryStoreAddresses), as well as the search array
- (void)viewDidLoad
{
[super viewDidLoad];
self.groceryStores = [NSArray arrayWithObjects:@"Safeway",@"Grocery Store 1",@"Grocery Store 2",@"Grocery Store 3",@"Grocery Store 4",@"Grocery Store 5",@"Grocery Store 6",@"Grocery Store 7",nil];
self.groceryStoreAddresses = [NSArray arrayWithObjects:@"1444 Shattuck Place, Berkeley, CA 94709",@"Address 1",@"Address 2",@"Address 3",@"Address 4",@"Address 5",@"Address 6",@"Address 7",nil];
self.searchGroceryStores = [NSMutableArray arrayWithCapacity:[self.groceryStores count]];
}
The search method
- (BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self.searchGroceryStores removeAllObjects] ;
for (NSString *groceryStore in self.groceryStores) {
NSRange result = [groceryStore rangeOfString:searchString options:
NSCaseInsensitiveSearch];
if (result.location != NSNotFound)
[self.searchGroceryStores addObject:groceryStore];
}
return YES;
}
Rows in section tweaked for search functionality
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [self.searchGroceryStores count];
}
else {
return [self.groceryStores count];
}
}
cellForRowAtIndexPath tweaked for search functionality
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCellAccessoryDisclosureIndicator"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [self.searchGroceryStores objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [self.groceryStoreAddresses objectAtIndex:indexPath.row];
}
else {
cell.textLabel.text = [self.groceryStores objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [self.groceryStoreAddresses objectAtIndex:indexPath.row];
}
// Configure the cell...
return cell;
}
Please help if you can. Thank you so much for your time, I appreciate it.
You need to keep a
searchGroceryStoreAddressesarray in parallel with yoursearchGroceryStoresarray. Example: