• UI进阶--UIPikcerView实现省市联动示例


    需求:
    1.显示2列数据,一列数据为省级城市,另外一列为省级对应下的地市级城市,并可供选择;
    2.选择省级城市时,对应的列显示为其省下的地市级城市,并可选择;
    3.重新选择省级城市时,对应的地市级城市显示为其省会城市(如地市级城市不在索引为0处);
    实现步骤:
    1、搭建界面;
    2、实现UIPikcerView的代理方法;
    3、响应选择事件;
    4、完成显示选择数据;
    具体代码:
    Model:
     1 //
     2 //  JWProvince.h
     3 //  12-27-Province
     4 //
     5 //  Created by xiaomoge on 14/12/27.
     6 //  Copyright (c) 2014年 xiaomoge. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 
    11 @interface JWProvince : NSObject
    12 @property (nonatomic,copy) NSString *name;
    13 @property (nonatomic,strong) NSArray *cities;
    14 - (instancetype)initWithDic:(NSDictionary *)dic;
    15 + (instancetype)provinceWithDic:(NSDictionary *)dic;
    16 + (NSMutableArray *)provinceList;
    17 @end
     1 //
     2 //  JWProvince.m
     3 //  12-27-Province
     4 //
     5 //  Created by xiaomoge on 14/12/27.
     6 //  Copyright (c) 2014年 xiaomoge. All rights reserved.
     7 //
     8 
     9 #import "JWProvince.h"
    10 
    11 @implementation JWProvince
    12 - (instancetype)initWithDic:(NSDictionary *)dic {
    13     if (self = [super init]) {
    14         [self setValuesForKeysWithDictionary:dic];
    15     }
    16     return self;
    17 }
    18 + (instancetype)provinceWithDic:(NSDictionary *)dic {
    19     return [[self alloc] initWithDic:dic];
    20 }
    21 
    22 + (NSMutableArray *)provinceList {
    23     NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"provinces" ofType:@"plist"]];
    24     
    25     NSMutableArray *tempArray = [NSMutableArray array];
    26     
    27     for (NSDictionary *dic in array) {
    28         JWProvince *province = [JWProvince provinceWithDic:dic];
    29         [tempArray addObject:province];
    30     }
    31     return tempArray;
    32 }
    33 @end

    Controller:

     1 //
     2 //  ViewController.m
     3 //  12-27-Province
     4 //
     5 //  Created by xiaomoge on 14/12/27.
     6 //  Copyright (c) 2014年 xiaomoge. All rights reserved.
     7 //
     8 
     9 #import "ViewController.h"
    10 #import "JWProvince.h"
    11 @interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
    12 @property (nonatomic,strong) NSArray *provinceDatas;
    13 @property (nonatomic,assign) NSInteger index;
    14 @end
    15 
    16 @implementation ViewController
    17 #pragma mark - 懒加载
    18 - (NSArray *)provinceDatas {
    19     if (!_provinceDatas) {
    20         _provinceDatas = [JWProvince provinceList];
    21     }
    22     return _provinceDatas;
    23 }
    24 #pragma mark - UIPickerView数据源
    25 //返回显示数据的列数
    26 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    27     return 2;
    28 }
    29 //返回每列中对应的行的数
    30 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    31     if (component == 0) {
    32         return self.provinceDatas.count;//第一列显示的个数
    33     }
    34     JWProvince *pro = self.provinceDatas[self.index];
    35     return pro.cities.count;//第二列显示的个数
    36 }
    37 #pragma mark - UIPickerView代理
    38 //返回显示每列以及每行的内容
    39 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    40     if (component == 0) {
    41         JWProvince *pp = self.provinceDatas[row];
    42         return pp.name;//第一列显示的内容
    43     }
    44     JWProvince *pro = self.provinceDatas[self.index];
    45     return pro.cities[row];//第二列显示的内容
    46 }
    47 #pragma mark - pickerView的选中方法
    48 //选中行时做的操作
    49 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    50     if (component == 0) {
    51         self.index = row;
    52         [pickerView reloadComponent:1];//重新加载对应的数据
    53         [pickerView reloadAllComponents];//重新加载所有的数据
    54         [pickerView selectRow:0 inComponent:1 animated:YES]; //不管之前第二列选中第几行,重新刷新数据后,都显示每二列的第一行
    55     }
    56 }
    57 @end
  • 相关阅读:
    「翻译」Unity中的AssetBundle详解(二)
    「翻译」Unity中的AssetBundle详解(一)
    [翻译]理解Unity的自动内存管理
    Unity3D集成腾讯语音GVoice SDK
    Unity插件之Unity调用C#编译的DLL
    最好用的Unity版本控制工具
    常见算法 php实现
    inotify+rsync实现文件双向实时同步
    30道Redis面试题
    mysql规范
  • 原文地址:https://www.cnblogs.com/xiaomoge/p/4200831.html
Copyright © 2020-2023  润新知