tags:

views:

233

answers:

2

I am trying to write a simple filter mechanism such that when the user types in a filter string the UIPicker filters it's data. I have it working almost.

This is my filter code and it works. My question is, once I have applied a filter to a datasource, how can I remove the predicate and restore the original dataset? I am using NSMutableArray for my datasource.

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] 'B'"];
[arrayCountryChoices filterUsingPredicate: predicate];
[pickerCountry reloadComponent:0];

Thank you in advance.

+1  A: 

Why don't you keep you original data on a NSArray, and the data that populates the UIPickerView on a NSMutableArray?

so, when you want the raw data you do like:


NSArray *myOriginalArray;

/*
Populate your array
*/

NSMutableArray *myMutableArray = [myOriginalArray mutableCopy];


/* Apply your filters on the mutable array
.
.
.
.
.
*/


/* When you need to restore the data you simply restore you original array */
myMutableArray = [myOriginalArray mutableCopy];

If you need to modify the original array during the lifetime of the application, you can set it as a mutable array. It will give you a chance o all the time that you need you restore your modified array to its previous content.

Cheers,
VFN

vfn
A: 

With this suggested approach I am getting this error. * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[UIView numberOfComponentsInPickerView:]:

I am not sure why. This is all the related code I can think of. I also checked my connections in IB. Any idea why?

//create data
    arrayCountryChoices = [NSArray arrayWithObjects:@"foo",@"bar",@"baz",nil];  

    //copy the original array to searchable array
    arraySearchResults = [arrayCountryChoices mutableCopy];

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {

    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {

    return [arraySearchResults count];

}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

    return [arraySearchResults objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {


    //grab the selected country
    strUserChoice = [arraySearchResults objectAtIndex:row]; 

}