用UIpickView实现省市的联动
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>
@property(strong,nonatomic)UIPickerView *pickView;
//定義一個可變數組用于存放省的數據
@property(strong,nonatomic)NSMutableArray *Statearry;
//定義一個可變數組用于存放市的數據
@property(strong,nonatomic)NSMutableArray *Citiesarry;
//定義一個集合分別存省和市的數據
@property(strong,nonatomic)NSArray *arry;
@end
?
#import "ViewController.h"
?
@interface ViewController ()
?
@end
?
@implementation ViewController
?
- (void)viewDidLoad {
? ? [super viewDidLoad];
?? ?
? ? //獲取數據
? ? NSString *path=[[NSBundle mainBundle] pathForResource:@"city" ofType:@"plist"];? ??
? ? //初始化省和市的數組
? ? self.Statearry=[NSMutableArray array];
? ? self.Citiesarry=[NSMutableArray array];
? ? //ayyr這個大數組存放所有的省市
? ? self.arry=[NSArray arrayWithContentsOfFile:path];
?
? ? //獲取省份的,將取出來的省份數據放在省的可變集合Statearry里
? ? for (NSDictionary *arr in self.arry)
? ? {
? ? ? ? [self.Statearry addObject:arr[@"State"]];
? ? }
?? ?
?? ?
? ? //創建pickView
? ? self.pickView=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 414, 200)];
? ? self.pickView.backgroundColor=[UIColor grayColor];
?? ?
? ? self.pickView.delegate=self;
? ? self.pickView.dataSource=self;
? ??[self.view addSubview:self.pickView];?
}?
#pragma mark 數據源 Method numberOfComponentsInPickerView
?- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
? ? //兩列
? ? return 2;
}
?
#pragma mark 數據源 Method pickerViewOfRows
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
? ? if (component==0)
? ? {
? ? ? ? //省份的個數
? ? ? ? return self.Statearry.count;
? ? }
? ? else
? ? {
? ? ? ? //市的個數
? ? ? ? return self.Citiesarry.count;
? ? }
}
?#pragma mark delegate 顯示信息的方法
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
? ? if (component==0)
? ? {
? ? ? ? //選擇的省份
? ? ? ? return self.Statearry[row];
? ? }
? ? else
? ? {
? ? ? ? //選擇的市
? ? ? ? return self.Citiesarry[row];
? ? }??
}
#pragma mark 選中行的信息
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
? ? if (component==0)
? ? {
?? ? ? ?
? ? ? ? //清空上一次市的那一列留下來的數據
? ? ? ? [self.Citiesarry removeAllObjects];
? ? ? ? //定義一個index,找出第一個滾動條里面的所有省對應的下標找出來,賦值給index
? ? ? ? NSInteger index=[pickerView selectedRowInComponent:0];
? ? ? ? //遍歷出所有市
? ? ? ? for (NSDictionary *city in self.arry[index][@"Cities"])
? ? ? ? {
? ? ? ? ? ? //將遍歷出來市追加到存放市的集合里
? ? ? ? ? ? [self.Citiesarry addObject:city[@"city"]];
? ? ? ? }
//? ? ? ? NSLog(@"%@",self.Citiesarry);
?? ?
? ? ? ? //更新第二個滾輪的數據
? ? ? ? [self.pickView reloadComponent:1];
? ? }
? ? else
? ? {
? ? ? ? //顯示取出來的省和市
? ? ? ? NSString *message=[NSString stringWithFormat:@"你選擇的是%@的%@?",self.Statearry[[pickerView selectedRowInComponent:0]],self.Citiesarry[row]];
?? ? ? ?
? ? ? ? //設置彈出框的標題
? ? ? ? UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
?? ? ? ?
? ? ? ? //設置按鈕名稱
? ? ? ? UIAlertAction *okAction=[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
?? ? ? ? ? ?
? ? ? ? }];
? ? ? ? //設置按鈕名稱
? ? ? ? UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
? ? ? ? //將按鈕加到提示框里面
? ? ? ? [alert addAction:okAction];
? ? ? ? [alert addAction:cancelAction];
? ? ? ? //
? ? ? ? [self presentViewController:alert animated:YES completion:nil];
? ? }? ? ? ?
}
- (void)didReceiveMemoryWarning {
? ? [super didReceiveMemoryWarning];
? ? // Dispose of any resources that can be recreated.
}
@end
?
轉載于:https://www.cnblogs.com/layios/p/5270277.html
總結
以上是生活随笔為你收集整理的用UIpickView实现省市的联动的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Redis
- 下一篇: JTable 一个最好的例子
