I have a table and search bar. The table is generated from a SQLite DB and the search bar searches those objects. The problem I had was when I wanted to know what was the index of the search result, if I just looked at the array of search results and compared to my DB table rows, it couldn't account for duplicate entries (http://stackoverflow.com/questions/11617247/how-can-i-figure-out-what-uitableviewcell-im-clicking-on-during-a-search), so then I implemented a method using indexesOfObjectsPassingTest
(http://stackoverflow.com/questions/11674498/how-can-i-determine-the-index-of-an-object-in-an-nsmutablearray-when-i-search-fo).
The problem now is, I get an NSIndexSet as my result. I want to be able to query this as I can an NSMutableArray but I know it's not possible, so I'm enumerating the IndexSet, which causes significant memory problems as there are over 5,000 possible results. Is there a better way to do this? I need to determine which items are search results, and then which particular result I'm looking at in the table.
//when the user searches, this function is called:
- (void) getGrapeMatches {
numSearchGrapes=0;
listOfTheGrapeIDs = [[NSMutableArray alloc] init];
for (NSString *str in listOfGrapes)
{
NSRange titleResultsRange = [str rangeOfString:searchBar.text options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0) {
[searchResult addObject:str];
numSearchGrapes++;
}
}
BOOL (^test)(id obj, NSUInteger idx, BOOL *stop);
test = ^ (id obj, NSUInteger idx, BOOL *stop) {
if ([searchResult containsObject: obj]) {
return YES;
}
return NO;
};
//this is the part that takes forever because I have over 5000 items in listOfGrapes
NSIndexSet *indexes = [listOfGrapes indexesOfObjectsPassingTest:test];
NSUInteger index=[indexes firstIndex];
while(index != NSNotFound)
{
NSString *tempString = [NSString stringWithFormat:@"%d",index];
[listOfTheGrapeIDs addObject:tempString];
index=[indexes indexGreaterThanIndex: index];
}
}
I rely on having listOfTheGrapeIDs
because it's the way I figure out what item I'm looking at in the table cell later on. I just wish there were an easier way to do this. Let me know if more code is necessary to help in understanding this problem.
reposting as answer:
Not sure I totally understand--but can't your database directly return the results of interest using a SQL query? Now you have the objects that match your search term.