Having Problems With UISearchBar in UITableViewController

I am trying to add a UISearchBar to my UITableView.

This is the method they use to search:

  • How to mock an object with OCMock which isn't passed as a parameter to method?
  • how to implemet Digital picture frame in ios app?
  • How to access iOS private APIs in Swift?
  • UINavigationItem Back Button touch area too large
  • How can I find out the Objective-C generics type?
  • How can i get the textfield value from tag in iPhone sdk?
  • - (void) searchTableView {
    
    NSString *searchText = searchBar.text;
    NSMutableArray *searchArray = [[NSMutableArray alloc] init];
    
    for (NSDictionary *dictionary in listOfItems)
    {
    NSArray *array = [dictionary objectForKey:@"Countries"];
    [searchArray addObjectsFromArray:array];
    }
    
    for (NSString *sTemp in searchArray)
    {
    NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
    
    if (titleResultsRange.length > 0)
    [copyListOfItems addObject:sTemp];
    }
    
    [searchArray release];
    searchArray = nil;
    }
    

    But I don’t have a dictionary, I just want to search in my array ‘exerciseArray’ which is init like this:

    NSString *path = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"];
    NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
    self.exerciseArray = rootLevel;
    [rootLevel release];
    

    When I NSLog it, I get:

    exerciseArray: (
            {
            exerciseName = "Balance Board";
        },
            {
            exerciseName = "Barbell Seated Calf Raise";
        },
            {
            exerciseName = "Calf Press On The Leg Press Machine";
        },
            {
            exerciseName = "Calf Raises- With Bands";
        },
    

    So can anyone help me modify the search method to fit the array I have? It seems to have a key in it.

    I tried this but it did not work and crashed at line NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

    with error *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance 0x2d6450

    - (void) searchTableView {
    
        NSString *searchText = searchBar.text;
        NSLog(@"exerciseArray: %@", exerciseArray);
        for (NSString *sTemp in listOfItems)
        {
            if (sTemp) {
                NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
    
                if (titleResultsRange.length > 0)
                    [copyListOfItems addObject:sTemp];
            }
        }
    }
    

    3 Solutions Collect From Internet About “Having Problems With UISearchBar in UITableViewController”

    Looks like you have an array of dictionaries with one key/value pair in each dictionary (exerciseName). so for your dataset, it should be this:

    for (NSDictionary *dictionary in exerciseArray)
    {
        NSArray *array = [dictionary objectForKey:@"exerciseName"];
        [searchArray addObjectsFromArray:array];
    }
    ...
    

    If you post your VC code related to the array and also your tableview datasource methods, I have can modify it and show you what you need to do.

    For searching from table view i have tried this and i got success hope it will help you……

    -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {
        self.searching=NO;
        NSString *originalString;
        [self.filteredProductArray removeAllObjects];
        for (int i=0; i<self.exerciseArray.count; i++)
        {
            originalString=[[[self.exerciseArray objectAtIndex:i] objectForKey:@"exerciseName"] lowercaseString];
            if (!([originalString rangeOfString:[searchText lowercaseString]].location==NSNotFound))
            {
                [self.filteredProductArray addObject:[self.exerciseArray objectAtIndex:i]];
            }
        }
        if ([searchText isEqual:@""])
        {
            self.searching=NO;
        }
        [self.tableView reloadData];
    }
    

    I believe NSPredicates are good way to search in arrays.. You can refer to this link.. Using NSPredicate to filter an NSArray based on NSDictionary keys