Apologies in advance if my question is unclear as I’m new to iOS programming (and programming in general).
Right now, I have a UITableView that displays an alphabetical NSMutableArray of the names of a user’s friends, something like:
friends: { @"Andy",@"Anand",@"Bob",@"Cat" };
I have another NSMutableArray that holds the matching ages of the user’s friends, something that the user never sees but is passed on to the previous view controller after the user picks the name of a friend. This ages array is something like:
friendsAges: {@"25",@"23",@"30",@"22"};
My question is, if I added a searchBar to the UITableview of the friends’ names, how do I make sure that the corresponding age object is sent back to the previous view controller if the user picks a name from the filtered array, searchResultsTableView?
For example, if there wasn’t a searchBar, picking “Bob” would also send “30” to the previous controller. But if the user types in “bo” and only Bob is shown in the filtered searchResultsTableView, then age “25” will be sent instead to the previous controller b/c it’s the first index.
Here’s my incorrect code:
if (tableView == self.searchDisplayController.searchResultsTableView) {
NSString *theFriend = [_filteredFriendArray objectAtIndex:indexPath.row];
// not sure what I should use instead for the _filteredFriendArray below
NSString *theFriendAge = [_filteredFriendArray objectAtIndex:indexPath.row];
[self.delegate friendPickerViewController:self
didSelectFriend:theFriend
didSelectFriendAge:theFriendAge];
} else { //if not using search bar and seeing filtered search results
NSString *theFriend = [friends objectAtIndex:indexPath.row];
NSString *theFriendAge = [friendsAge objectAtIndex:indexPath.row];
[self.delegate friendPickerViewController:self
didSelectFriend:theFriend
didSelectFriendAge:theFriendAge];
}
I feel like it might be easier to use an NSDictionary instead of trying to match the index of a filtered search view back to main array, but am not sure how to best do that.
Any help would be appreciated!
You are right, it is easier to use NSDictionary. Each person can be represented by an NSDictionary like @{@”name”:@”Stephen Hawking”, @”age”:70}. Populate your friends table view with [personDict objectForKey:@”name”] and on selection get the value for [person objectForKey:@”age”] and pass it back to the previous view controller.