I have this program with tableView as my first view. I also implemented (or at least tried) the search bar on top of the view. You used several hours to find a solution, but without positive results.
#import "FirstViewController.h" #import "NSDictionary-MutableDeepCopy.h" @implementation FirstViewController @synthesize listData, table, search, allNames, names, keys; #pragma mark - #pragma mark Custom Methods - (void)resetSearch { NSMutableDictionary *allNamesCopy = [self.allNames mutableDeepCopy]; self.names = allNamesCopy; [allNamesCopy release]; NSMutableArray *keyArray = [[NSMutableArray alloc] init]; [keyArray addObjectsFromArray:[[self.allNames allKeys] sortedArrayUsingSelector:@selector(compare:)]]; self.keys = keyArray; [keyArray release]; } -(void)handleSearchForTerm:(NSString *)searchTerm { NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init]; [self resetSearch]; for (NSString *key in self.keys) { NSMutableArray *array = [names valueForKey:key]; NSMutableArray *toRemove = [[NSMutableArray alloc] init]; for (NSString *name in listData) { if ([name rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location == NSNotFound) [toRemove addObject:name]; } if ([array count] == [toRemove count]) [sectionsToRemove addObject:key]; [array removeObjectsInArray:toRemove]; [toRemove release]; } [self.keys removeObjectsInArray:sectionsToRemove]; [sectionsToRemove release]; [table reloadData]; } - (void)viewDidLoad { NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"]; NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path]; self.names = dict; self.allNames = dict; [dict release]; [self resetSearch]; [table reloadData]; [table setContentOffset:CGPointMake(0.0, 44.0)animated:NO]; self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]; NSArray *array = [[NSArray alloc] initWithObjects:
Ok guys. My problem is that this does not help the search function to work. In addition, I get a cigar SIGABRT in this line:
NSString *key = [keys objectAtIndex:section];
I need help with two things:
1: I need to get this SIGABRT.
Error log message: * Application terminated due to an uncaught exception 'NSRangeException', reason: '* - [NSMutableArray objectAtIndex:]: index 0 outside for an empty array'
That is, I do not store data in keys. how would i do that? and what will i store?
2: want the search function to search in my listData array!
Thanks in advance - I hope you can help!
source share