tags:

views:

218

answers:

1

I am trying to develop a simple search mechanism on a UIPicker. The approach I am using is to keep two arrays. My problem is that for some reason I am getting this run-time error.

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIView numberOfComponentsInPickerView:]

Here are the array declarations.

//data source for UIPicker NSArray
*arrayCountryChoices;

//search results buffer 
NSMutableArray *arraySearchResults; 

//properties
@property(nonatomic,retain) NSArray*arrayCountryChoices; 
@property(nonatomic,retain) NSMutableArray *arraySearchResults;

Here is where I initialize the data

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

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

An the picker methods.

- (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];
}

Here is the search code for completeness although not really relevant yet as the app dies before we ever get here.

//filter on search term
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", strSearchText];
[arraySearchResults filterUsingPredicate: predicate];
[pickerCountry reloadComponent:0];

I have also dragged datasource and delegate connections from the UIPicker to Files Owner in Interface Builder. Any ideas? Thanks in advance.

A: 

Looks like you've got the picker's data source set to something other than the object that implements the code you've posted there—apparently a UIView somewhere. Make sure the picker's outlets point to your actual data-source object.

Noah Witherspoon
It does seem like that but I have checked within IB that the picker datasource and delegate are set to Files Owner. Mmmm....
What code are you using to instantiate the stuff in the NIB?
Noah Witherspoon
Also, the code posted above is from the CountryViewController. Within this objects, viewdidload method I put pickerCountry.dataSource = self; pickerCountry.delegate = self;This still results in a run-time error that I do not understand. Any ideas?????
I'm not doing anything different that usual in terms of then NIB. In fact, I don't see any explicit code of this type other than this.- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { // Custom initialization self.title = @"Countries"; } return self;}
I think I have located part of the problem. If I comment out these lines. //create data //arrayCountryChoices = [[NSArray alloc] initWithObjects:@"foo",@"bar",@"baz",nil]; //copy the original array to searchable array //arraySearchResults = [NSMutableArray arrayWithArray:arrayCountryChoices];Then, and empty picker loads. With the lines of code I get a run-time error. Mmmmm?????
It looks like the problem might be with the searchArray. I thought at first it was not being allocated. So, I added this linearraySearchResults = [[NSMutableArray alloc arrayWithArray:arrayCountryChoices];This did not work. However, if I change all the Picker methods within the view controller to use the arrayCountryChoices, there is no run-time error. For that reason, I suspect the searchArray does not have any data in it despite this call.arraySearchResults = [[NSMutableArray alloc arrayWithArray:arrayCountryChoices];Can anyone comment on that possibility?