UIPickerView is a very common UI control, which is necessary for various shopping platforms to select addresses. Let's talk about the specific use
First, UIPickerView is created. Like most controls, it allocates memory and sets the location size.
It is important to set up the agent and data source and obey the agent and data source protocol
<UIPickerViewDelegate,UIPickerViewDataSource>
There are two methods that must be implemented in the data source
//Set number of columns - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return count; } //Sets the number of items contained in the specified column - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
}
UIPickerView can only set a single column, which is called component. At this time, the number of returned columns is 1. More than one column is needed for a single column. At this time, set the return value to the number of columns you need.
In UIPickerView datasource data source protocol, only several columns and the number of items in each column are provided for UIPickerView, and the options displayed in each row are set through the methods in UIPickerView delegate protocol.
//Set what each option displays - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ } //Get the currently selected option of the user - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ }
UIPickerView basic properties and methods:
Set up data source and proxy objects
@property(nullable,nonatomic,weak) id<UIPickerViewDataSource> dataSource; @property(nullable,nonatomic,weak) id<UIPickerViewDelegate> delegate;
Reload columns:
- (void)reloadAllComponents; - (void)reloadComponent:(NSInteger)component;
Get the currently selected option sequence number:
- (NSInteger)selectedRowInComponent:(NSInteger)component;
Specify that the selected item is displayed in the middle. Generally, set the first item in the middle:
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;
Here is a simple, complete example:
#import "ViewController.h" @interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource> @property(nonatomic,strong)UIPickerView *pickerVIew; @property(nonatomic,strong)NSDictionary *dictionary; @property(nonatomic,strong)NSArray *provinceArray; @property(nonatomic,copy)NSString *selectedProvince; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.dictionary = @{@"Jiangsu":@[@"Nanjing",@"Xuzhou",@"Zhenjiang",@"Wuxi",@"Changzhou"],@"Hebei":@[@"Shijiazhuang",@"Baoding",@"Chengde",@"Cangzhou",@"Qinghuangdao"]}; //Get all provinces in the dictionary and save them by sorting self.provinceArray = [[self.dictionary allKeys] sortedArrayUsingSelector:@selector(compare:)]; self.selectedProvince = self.provinceArray[0]; [self.view addSubview:self.pickerVIew]; } //Lazy loading - (UIPickerView *)pickerVIew{ if (_pickerVIew == nil) { self.pickerVIew = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, 400)]; _pickerVIew.layer.masksToBounds = YES; _pickerVIew.layer.borderWidth = 1; _pickerVIew.delegate = self; _pickerVIew.dataSource = self; } return _pickerVIew; } #pragma mark ------- dateSource&&Delegate -------- //Set number of columns - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 2; } //Sets the number of items contained in the specified column - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ if (component == 0) { return self.provinceArray.count; } return [self.dictionary[self.selectedProvince] count]; } //Set what each option displays - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ if (component == 0) { return self.provinceArray[row]; } return [self.dictionary[self.selectedProvince] objectAtIndex:row]; } //User to select - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if (component == 0) { self.selectedProvince = self.provinceArray[row]; [self.pickerVIew reloadComponent:1]; //Setting the second column is always the first [self.pickerVIew selectRow:0 inComponent:1 animated:YES]; } } @end