iOS省市区三级联动UIPickerView(含省市区id)

导读:

实现效果如下图:

用法:

直接继承MOFSAddressPickerView类,初始化即可。方法func show()调出pickerView

var pickerView = MOFSAddressPickerView();

pickerView.mofsDelegate = self;

//MARK: - MOFSAddressPickerViewDelegate

func selectedAddress(province: String, city: String, area: String, id: String) {

}

点击确定之后协议返回省市区名称及id;



MOFSAddressPickerView代码:

import UIKit

let UISCEEN_WIDTH = UIScreen.mainScreen().bounds.width;

let UISCEEN_HEIGHT = UIScreen.mainScreen().bounds.height;

protocol MOFSAddressPickerViewDelegate {  

func selectedAddress(province: String, city: String, area: String, id: String); 

 }

class MOFSAddressPickerView: UIPickerView,UIPickerViewDelegate,UIPickerViewDataSource {      

var mofsDelegate:MOFSAddressPickerViewDelegate!   

var toolBar = UIToolbar();   

var containerView = UIView(); //背景View   

var commitBar:UIBarButtonItem!; //确定UIBarButtonItem   

var cancelBar:UIBarButtonItem!; //取消UIBarButtonItem       

var provinceArr:Array= Array();

var selectedIndex_province = 0; //选中的省

var selectedIndex_city = 0; //选中的市

var selectedIndex_area = 0; //选中的区

/*

*pickerView高度

*默认216;

*/

var height:CGFloat! {

didSet {

self.frame = CGRectMake(0, UISCEEN_HEIGHT - height, UISCEEN_WIDTH, height);

toolBar.frame = CGRectMake(0, UISCEEN_HEIGHT - height - 44, UISCEEN_WIDTH, 44);

containerView.frame = CGRectMake(0, 0, UISCEEN_WIDTH, UISCEEN_HEIGHT - height - 44);

}

}

/*

*cancelBar的title

*默认为“取消”

*/

var cancelBarTitle:String! {

didSet {

cancelBar.title = cancelBarTitle;

}

}

/*

*cancelBar的文字颜色

*默认UIColor(hexString: "0079fe")

*/

var cancelBarColor:UIColor! {

didSet {

cancelBar.tintColor = cancelBarColor;

}

}

/*

*commitBar的title

*默认为“确定”

*/

var commitBarTitle:String! {

didSet {

commitBar.title = commitBarTitle;

}

}

/*

*commitBar的文字颜色

*默认UIColor(hexString: "0079fe")

*/

var commitBarColor:UIColor! {

didSet {

commitBar.tintColor = commitBarColor;

}

}

//MARK: - self.init

override init(frame: CGRect) {

var initialFrame:CGRect!;

if (CGRectIsEmpty(frame)) {

initialFrame = CGRectMake(0, UISCEEN_HEIGHT - 216, UISCEEN_WIDTH, 216);

} else {

initialFrame = frame;

}

initialFrame = CGRectMake(0, UISCEEN_HEIGHT - initialFrame.height, UISCEEN_WIDTH, initialFrame.height);

super.init(frame: initialFrame);

self.backgroundColor = UIColor(hexString: "#F6F6F6");

self.delegate = self;

self.dataSource = self;

self.getData();

self.initToolBar();

self.initContainerView();

}

required init?(coder aDecoder: NSCoder) {

fatalError("init(coder:) has not been implemented")

}

//MARK: - getData

func getData() {

let path = NSBundle.mainBundle().pathForResource("Province&City&District", ofType: "plist");

let dic = NSDictionary(contentsOfFile: path!);

for (var i = 0; i < dic?.allKeys.count; i++) {

let model = Model(json: JSON(dic!["\(i)"]!));

provinceArr.append(model);

}

}

//MARK: - show

func show() {

let window = UIApplication.sharedApplication().keyWindow;

window?.addSubview(containerView);

window?.addSubview(self);

window?.addSubview(toolBar);

}

//MARK: - initToolBar

func initToolBar() {

toolBar.frame = CGRectMake(0, UISCEEN_HEIGHT - self.frame.height - 44, UISCEEN_WIDTH, 44);

commitBar = UIBarButtonItem(title: "    完成    ", style: UIBarButtonItemStyle.Done, target: self, action: "commitAction");

commitBar.tintColor = UIColor(hexString: "0079fe");

cancelBar = UIBarButtonItem(title: "    取消    ", style: UIBarButtonItemStyle.Done, target: self, action: "cancelAction");

cancelBar.tintColor = UIColor(hexString: "0079fe");

let nullBar = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: "");

toolBar.items = [cancelBar,nullBar,commitBar];

toolBar.backgroundColor = self.backgroundColor;

let lineView = UIView(frame: CGRectMake(0, 43.5, UISCEEN_WIDTH, 0.5));

lineView.backgroundColor = UIColor(hexString: "#D3D3D3");

toolBar.addSubview(lineView);

toolBar.bringSubviewToFront(lineView);

}

//MARK: - initContainerView

func initContainerView() {

containerView.frame = CGRectMake(0, 0, UISCEEN_WIDTH, UISCEEN_HEIGHT - self.frame.height - 44)

containerView.backgroundColor = UIColor.blackColor();

containerView.alpha = 0.4;

containerView.userInteractionEnabled = true;

containerView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "cancelAction"));

}

//MARK: - commitAction

func commitAction() {

self.cancelAction();

let province = provinceArr[selectedIndex_province].name;

var id = provinceArr[selectedIndex_province].id;

var city = "";

var area = "";

if (provinceArr[selectedIndex_province].arr.count > 0) {

city = provinceArr[selectedIndex_province].arr[selectedIndex_city].name;

id = provinceArr[selectedIndex_province].arr[selectedIndex_city].id;

if (provinceArr[selectedIndex_province].arr[selectedIndex_city].arr.count > 0) {

area = provinceArr[selectedIndex_province].arr[selectedIndex_city].arr[selectedIndex_area].name;

id = provinceArr[selectedIndex_province].arr[selectedIndex_city].arr[selectedIndex_area].id;

}

}

mofsDelegate.selectedAddress(province, city: city, area: area, id: id);

}

//MARK: - cancelAction

func cancelAction() {

toolBar.removeFromSuperview();

containerView.removeFromSuperview();

self.removeFromSuperview();

}

//MARK: - UIPickerViewDataSource

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {

return 3;

}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

switch (component) {

case 0:

return provinceArr.count;

case 1:

return provinceArr[selectedIndex_province].arr.count;

case 2:

if (provinceArr[selectedIndex_province].arr.count == 0) {

return 0;

}

return provinceArr[selectedIndex_province].arr[selectedIndex_city].arr.count;

default:

return 0;

}

}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

switch (component) {

case 0:

return provinceArr[row].name;

case 1:

return provinceArr[selectedIndex_province].arr[row].name;

case 2:

return provinceArr[selectedIndex_province].arr[selectedIndex_city].arr[row].name;

default:

return nil;

}

}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

switch (component) {

case 0:

selectedIndex_province = row;

selectedIndex_city = 0;

selectedIndex_area = 0;

pickerView.reloadComponent(1);

pickerView.reloadComponent(2);

pickerView.selectRow(0, inComponent: 1, animated: false);

pickerView.selectRow(0, inComponent: 2, animated: false);

case 1:

selectedIndex_city = row;

selectedIndex_area = 0;

pickerView.reloadComponent(2);

pickerView.selectRow(0, inComponent: 2, animated: false);

case 2:

selectedIndex_area = row;

default:

break;

}

}

}

下载代码:https://github.com/memoriesofsnows/AreaPickerView

欢迎关注我的

CSDN:http://blog.csdn.net/luoyuant

新浪博客:http://blog.sina.com.cn/memoriesofsnows

网易博客:http://memoriesofsnows.blog.163.com/
简书:http://www.jianshu.com/users/f4284f2cc646/latest_articles
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值