IOS开发ios开发学习系列首页投稿(暂停使用,暂停投稿)

UIPickerView选择位置(OC版)

2016-05-27  本文已影响3080人  疯狂的喵喵

iOS9使用UIPickerView地点选择器

最近公司在做类似淘宝添加地址的位置选择器,研究了一下,下面把实现过程分享出来。

1.首先演示下效果:

Simulator Screen Shot 2016年5月27日 上午11.52.27.png Simulator Screen Shot 2016年5月27日 上午11.52.32.png

2.下面说下实现过程:

// 1.设置列的返回数量
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}
// 2.设置列里边组件的个数 component:组件
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    //如果是第一列
    if (component == 0)
    {
        //返回省的个数
        return self.provinceArray.count;
    }
    //如果是第二列
    else if (component == 1)
    {
        //返回市的个数
        return self.cityArray.count;
    }
    else
    {
        //返回县区的个数
        return self.countyArray.count;
    }
}

// 3.返回组件的标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0) {
        // 设置第0列的标题信息
        NSDictionary *provinceDict = [self.provinceArray objectAtIndex:row];
        NSString *name = provinceDict[@"n"];
        return name;
    } else if (component == 1) {
        // 设置第1列的标题信息
        NSDictionary *cityDict = [self.cityArray objectAtIndex:row];
        NSString *name = cityDict[@"n"];
        return name;
    } else {
        // 设置第2列的标题信息
        NSDictionary *countryDict = [self.countyArray objectAtIndex:row];
        NSString *name = countryDict[@"n"];
        return name;
    }
}

#pragma mark - 加载数据
- (void)loadData
{
    // 从MainBundle中加载文件
    NSString *path = [[NSBundle mainBundle] pathForResource:@"citylist" ofType:@"data"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSArray  *jsonArray = [NSJSONSerialization
                           JSONObjectWithData:data options:NSJSONReadingAllowFragments
                           error:nil];
    // 取出默认的省市信息
    self.provinceArray = jsonArray;
    
    // 取出默认的城市信息
    NSDictionary *provinceDict = [self.provinceArray firstObject];
    self.cityArray = provinceDict[@"l"];
    
    // 取出默认的区信息
    NSDictionary *cityDict = [self.cityArray firstObject];
    self.countyArray = cityDict[@"l"];
}

//选择器选择的方法  row:被选中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    //选择第0列执行的方法
    if (component == 0) {
        [pickerView selectedRowInComponent:0];
        
        /**
         *  选中第0列时需要刷新第1列和第二列的数据
         */
        NSDictionary *provinceDict = [self.provinceArray objectAtIndex:row];
        self.cityArray = provinceDict[@"l"];
        [pickerView reloadComponent:1];
        
        NSDictionary *cityDict = [self.cityArray firstObject];
        self.countyArray = cityDict[@"l"];
        [pickerView reloadComponent:2];
        
    } else if (component == 1) {
        [pickerView selectedRowInComponent:1];
        
        /**
         *  选中第一列时需要刷新第二列的数据信息
         */
        NSDictionary *cityDict = [self.cityArray objectAtIndex:row];
        self.countyArray = cityDict[@"l"];
        [pickerView reloadComponent:2];
        
    } else if (component == 2) {
        [pickerView selectedRowInComponent:2];
        
    }
}

// 作为TextField的弹出视图的工具条
- (UIToolbar *)toolbar
{
    if (_toolbar == nil) {
        _toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 49)];
        
        UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
        UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        UIBarButtonItem *certain = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(certain)];

        _toolbar.items = @[cancel,flexibleSpace,certain];
    }
    return _toolbar;
}

// 点击了取消
- (void)cancel
{
    [self.textField resignFirstResponder];
}

// 点击了确定
- (void)certain
{
    // 获取当前选中的信息
    NSInteger proviceIndex = [self.pickerView selectedRowInComponent:0];
    NSInteger cityIndex = [self.pickerView selectedRowInComponent:1];
    NSInteger countryIndex = [self.pickerView selectedRowInComponent:2];

    NSString *provice = self.provinceArray[proviceIndex][@"n"];
    NSString *city = self.cityArray[cityIndex][@"n"];
    NSString *country = self.countyArray[countryIndex][@"n"];
    
    NSLog(@"%@,%@,%@",provice,city,country);
    
    // 弹出提示
    NSString *message = [NSString stringWithFormat:@"您选择的是:%@-%@-%@",provice,city,country];
    
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:(UIAlertControllerStyleAlert)];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        return;
    }];
    [alertVc addAction:cancel];
    
    [self presentViewController:alertVc animated:YES completion:nil];

}

3.示例代码中我已经对UIPickerView做了封装,单独抽取在CWPickerView类中,调用时直接初始化UIPickerView即可:

   CWPickerView *pickerView = [[CWPickerView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)];
    self.pickerView = pickerView;
    // 将pickerView作为UITextFiled的输出View
    self.textField.inputView = pickerView;
    // 获取当前选中的信息
    NSDictionary *info = [self.pickerView getCurrentSelectedInfo];
    // 获取当前选中的信息
    NSString *provice = info[@"province"];
    
    NSString *city = info[@"city"];
    
    NSString *country = info[@"country"];
    
    // 弹出提示
    NSString *message = [NSString stringWithFormat:@"您选择的是:%@-%@-%@",provice,city,country];

4.到这里差不多就结束了,如果有什么不对的地方,欢迎大家指正,如果对大家有帮助请给个喜欢或关注,非常感谢!下面附上代码地址:

GitHub地址

上一篇下一篇

猜你喜欢

热点阅读