开发者

Having Problems With UISearchBar in UITableViewController

开发者 https://www.devze.com 2023-03-16 00:23 出处:网络
I am trying to add a UISearchBar to my UITableView. This is the method they use to search: - (void) searchTableView {

I am trying to add a UISearchBar to my UITableView.

This is the method they use to search:

- (void) searchTableView {

NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];

for (NSDictionary *dictionary in listOfItems)
{
NSArra开发者_JAVA技巧y *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];
        }
    }
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号