IOS之簡單選擇器UIPickerView(省份+城市)
阿新 • • 發佈:2019-01-22
先介紹一下我們要實現什麼功能。有1個選擇器,有左右兩個輪子,做輪子選擇省份,右輪子選擇城市,其中選擇省份時,右邊的城市會自動更新。
1、首先,對UIPickerView繫結Delegate和DataSource到相應的ViewController。此處不再贅述。可以用程式碼或者Interface介面設定。
2、首先實現資料的初始化。
(1)在.h檔案中定義如下變數。其中provinces_cities.plist請見附件。
Cpp程式碼- @interface IkrboyViewController : UIViewController{
-
NSDictionary *dict;//用於儲存省份-城市的資料
- NSArray *provinceArray;//省份的陣列
- NSArray *cityArray;//城市的陣列,在接下來的程式碼中會有根據省份的選擇進行資料更新的操作
- }
(2)在.m的viewDidLoad方法中加上初始化資料的處理。具體處理在initPicker方法
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
-
[self initPicker];
- }
- //初始化PickerView使用的資料來源
- -(void)initPicker{
- NSBundle *bundle = [NSBundle mainBundle];
- NSString *plistPath = [bundle pathForResource:@"provinces_cities" ofType:@"plist"];
- dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
- provinceArray = [dict allKeys];
-
NSInteger selectedProvinceIndex = [self.pickerView selectedRowInComponent:0];
- NSString *seletedProvince = [provinceArray objectAtIndex:selectedProvinceIndex];
- cityArray = [dict objectForKey:seletedProvince];
- NSLog(@"%d",[provinceArray count]);
- }
3、將資料繫結到UIPickerView
Cpp程式碼- //以下3個方法實現PickerView的資料初始化
- //確定picker的輪子個數
- - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
- return 2;
- }
- //確定picker的每個輪子的item數
- - (NSInteger)pickerView:(UIPickerView *)pickerView
- numberOfRowsInComponent:(NSInteger)component {
- if (component == 0) {//省份個數
- return [provinceArray count];
- } else {//市的個數
- return [cityArray count];
- }
- }
- //確定每個輪子的每一項顯示什麼內容
- #pragma mark 實現協議UIPickerViewDelegate方法
- -(NSString *)pickerView:(UIPickerView *)pickerView
- titleForRow:(NSInteger)row forComponent:(NSInteger)component {
- if (component == 0) {//選擇省份名
- return [provinceArray objectAtIndex:row];
- } else {//選擇市名
- return [cityArray objectAtIndex:row];
- }
- }
4.隨時監聽UIPickerView的滾動。
Cpp程式碼- //監聽輪子的移動
- - (void)pickerView:(UIPickerView *)pickerView
- didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
- if (component == 0) {
- NSString *seletedProvince = [provinceArray objectAtIndex:row];
- cityArray = [dict objectForKey:seletedProvince];
- //重點!更新第二個輪子的資料
- [self.pickerView reloadComponent:1];
- NSInteger selectedCityIndex = [self.pickerView selectedRowInComponent:1];
- NSString *seletedCity = [cityArray objectAtIndex:selectedCityIndex];
- NSString *msg = [NSString stringWithFormat:@"province=%@,city=%@", seletedProvince,seletedCity];
- NSLog(@"%@",msg);
- }
- else {
- NSInteger selectedProvinceIndex = [self.pickerView selectedRowInComponent:0];
- NSString *seletedProvince = [provinceArray objectAtIndex:selectedProvinceIndex];
- NSString *seletedCity = [cityArray objectAtIndex:row];
- NSString *msg = [NSString stringWithFormat:@"province=%@,city=%@", seletedProvince,seletedCity];
- NSLog(@"%@",msg);
- }
- }
附加說明:利用下面的程式碼,獲得UIPickerView的不同Component(滾輪)的選中的index。
Cpp程式碼- NSInteger selectedProvinceIndex = [self.pickerView selectedRowInComponent:0];
- NSInteger selectedCityIndex = [self.pickerView selectedRowInComponent:1];