Reputation: 105
I want to add a search to filter table view. I have an idea how to do it, but as my cell is completely custom, i am not sure how to filter by name, and get all the other data from the NSDictionary
depending on name.
NSString *name = [searchResults objectAtIndex:indexPath.row];
This returns the name of the cell.textLabel
, after filtered correctly, but i need to regain all the information, as in my dictionary:
admin = ;
approved = ;
business = "";
category = ;
description = "";
distance = "";
email = "";
favs = ;
id = ;
image1 = "";
lat = "";
location = "";
lon = "";
name = "";
password = "";
rating = "";
telephone = "";
website = "";
How can i regain all the information above, from just getting the business name, if that makes sense?
Upvotes: 0
Views: 1648
Reputation: 2040
More information is needed, but if I interpret what you're doing correctly, t seems like you're extracting the name string from an object, then populating an array with those name strings as a data source for your UITableView. Then you're trying to work backwards from the name to get the original object.
If that's the case, why not just populate the array with the original objects to back the data store, then you can easily get the object by row based on the selected row of the UITableView. You just need to update where you set up the cell contents to pull the name field from the object rather than from a separate array.
Example:
// Defined in your class interface:
NSMutableArray *searchResults = [[NSMutableArray alloc] init];
NSMutableArray *myArray; // contains all of your objects, could be a dictionary as well
- (void) searchTableView {
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
[searchArray addObjectsFromArray:myArray];
for (myObject *anObject in searchArray) {
NSString *objectName = [myObject name];
NSRange resultsRange = [objectName rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (resultsRange.length > 0)
[searchResults addObject:anObject];
}
searchArray = nil;
}
Here we start with an array of all your objects, myArray (if they are stored in a Dictionary, just convert to an array in the next step).
A new array, searchArray, is populated with all of the objects from your main array (as shown) or dictionary (additional conversion).
In the loop, the name field for each object in the searchArray is compared against the text from the searchBar. If the searchBar contents exist within the name field, the object is added to the results array, searchResults.
Use searchResults as the data source for your UITableView, setting the text of each cell to the name of the corresponding object within searchResults. Now each row of the UITableView is directly related to your objects.
Note that I originally learned this method via the following site. I recommend working through their tutorial - it's quick and demonstrates some other nice features to use in conjunction with the search setup. Just note that it was written before ARC, so you'll need to leave out some of the release
statements, etc. assuming you're using ARC.
Upvotes: 3