I am trying to set up a UIPickerView with a UISearchBar that will help to find the info of states. I have the UIPickerView working beautifully. However, I can not get the searchbar to function with the picker. I have set the Searchbar delegate to the main controller.
Here is the code that is supposed to do it, I know at the moment it is filtering and removing some (would like it to just jump to the correct name as I type it without filtering). This doesn't update. Can anyone direct me to how to finish this out and how to correctly jump to the state without filtering?
This is for Xcode 5 IOS 7
The Array ListOfStates is the filtered array
and States is the main array
Here is the code that is supposed to do it, I know at the moment it is filtering and removing some (would like it to just jump to the correct name as I type it without filtering). This doesn't update. Can anyone direct me to how to finish this out and how to correctly jump to the state without filtering?
This is for Xcode 5 IOS 7
The Array ListOfStates is the filtered array
and States is the main array
Code:
(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (searching) {
return _ListOfStates.count;
}else{
return _States.count;
}
}
#pragma mark Picker Delegate Methods
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (searching) {
return [_ListOfStates objectAtIndex:row];
} else {
return [_States objectAtIndex:row];
}
}
(void)searhBar:(UISearchBar *)SearchBar textDidChange:(NSString *) searchText {
[_ListOfStates removeAllObjects];
if (searchText.length == 0) {
searching = NO;
} else {
searching = YES;
for (NSString *StateName in _States) {
NSRange stateNameRange = [StateName rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (stateNameRange.location != NSNotFound) {
[_ListOfStates addObject:StateName];
}
}
}
[_StatePickerView reloadAllComponents];
}
Last edited: