Question

I see item UISearchBar search two arrays and very much items and not found solution, the problem its similar, have two NSMutablesArrays "subdetail" and "sublista" its show in Cell cell.textLabel and cell.detailTextLabel. I try UISearchbar but i tray with NSPredicate and not run, try with NSRange and have more errors, i am desperate.PLEASE help me, any comments agree.

This its my code in Search:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSMutableDictionary *playas = [NSMutableDictionary dictionaryWithObjectsAndKeys:sublista, @"nombre", subdetail, @"localidad", nil];
    [alldata addObject:playas];
    for(NSDictionary *playas in alldata){
        NSString *nombre = [playas objectForKey:@"nombre"];
        NSRange nombreRange = [[nombre lowercaseString] rangeOfString:[searchText lowercaseString]];
        if(nombreRange.location != NSNotFound)
            [filteredList addObject:playas];
    }
}

Add rest of code .m and .h

https://dl.dropboxusercontent.com/u/6217319/BuscarViewController.h https://dl.dropboxusercontent.com/u/6217319/BuscarViewController.m

Thanks in advance. BEST REGARDS

Was it helpful?

Solution

Your code has a lot of issues with it. I don't know which ones are actually breaking it, but any of these issues could be causing severe problems.

First, the code sample you provided doesn't provide declarations for a lot of hte objects you're referencing. No way this could possibly compile as given. If these are properties of the view controller instance, you need to use the accessor methods -- self.alldata or [self alldata], whichever you prefer.

Second, it looks like you're just adding to these properties. If you never reset their contents, every time this method is called, you're going to increase the size of your data set -- looking at your code, possibly recursively.

Third, you try to merge the two datasets together, and then try to search through only one of them. Either don't merge, or search through both seperately and then merge the results. As it is, what you're doing won't work.

Fourth, your table view should really only be displaying one type of data, so you shouldn't need to merge.

Edit:

Based on the code samples you've provided, your entire VC is going to need restructuring. Given how much of your code is written in what appears to be spanish, I'm doing a bit of guesswork at what you're actually doing.

First off, assuming you're using a modern version of xcode, get rid of the @synthesize in the .m file (it's no longer needed anymore). That will cause every single place you're using the actual ivar instead of a proper getter to turn into an error, so you can fix them quickly. iVars will by default use a prefixed underscore of their property name, so you can still access them if you have to -- but only by explicitly accessing the ivar instead of the property.

Second, you should restructure how you handle the data. I don't know where you get your data, but it looks like the various objects are fairly consistent. You should go ahead and either create a new class to hold all the data, or just put them all into a single dictionary. At that point, the 'alldata' property should, in fact, be an array of all valid data. What you should do is then have a filtered list of data (filteredData would be a good name), and you place whatever data matches the search criteria in there. Just remember to either reload the table or update it appropriately as items move into and out of the filtered list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top