博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UIPickerView的使用
阅读量:4577 次
发布时间:2019-06-08

本文共 2098 字,大约阅读时间需要 6 分钟。

  要想使用UIPickerView,必须要知道UIPickerView的协议UIPickerViewDataSource,UIPickerViewDelegate,协议中主要的方法如下:

1、- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;//一共有多少列

2、- (NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;//第component列第row行显示什么。

3、- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;//选中了第component列的第row行

4、- (void)reloadComponent:(NSInteger)component;//重新加载第component列数据

下面通过一个例子详细说明下UIPickerView的使用,例子是开发中常用的级联操作,选择第一列的省,第二列加载相应的市。

#pragma UIPickerViewDataSource 代理方法

  // returns the number of 'columns' to display. 一共有多少列

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

    return  2;

}

 

// returns the # of rows in each component..第几列显示多少行

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

    if (component == 0) {

        // 第一列有多少个省份

        return _provinces.count;

    } else {

        // 1.获得选中了哪一个省

        int pIndex = [pickerView selectedRowInComponent:0];

        // 2.取出省份模型

        FCProvince *p = _provinces[pIndex];

        // 3.返回当前省份城市的个数

        return p.cities.count;

    }

}

 

//第n列第n行显示什么

- (NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

    if (component == 0) { // 省

        FCProvince *pro = self.provinces[row];

        return pro.name;

    } else  { // 市

        // 1.获得选中了哪一个省

        int pIndex = [pickerView selectedRowInComponent:0];

        // 2.取出省份模型

        FCProvince *p = _provinces[pIndex];

        // 3.返回对应行的城市名称

        return p.cities[row];    }

   

}

 

/**

 *  选中了第component列的第row行

 */

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{

    if (component == 0) {

        // 刷新第1列的数据(重新刷新数据,重新调用数据源和代理的相应方法获得数据)

        [pickerView reloadComponent:1];

        // 选中第1列的第0行

        [pickerView selectRow:0 inComponent:1 animated:YES];

       }

        // 1.获得选中了哪一个省

        int pIndex = [pickerView selectedRowInComponent:0];

        FCProvince *p = _provinces[pIndex];

        self.lblProvince.text = p.name;

       int cIndex = [pickerView selectedRowInComponent:1];

       self.lblCity.text = p.cities[cIndex];

   

}

 

转载于:https://www.cnblogs.com/fengchao1000/p/4896097.html

你可能感兴趣的文章
hash冲突的解决方法
查看>>
Asp.Net webconfig中使用configSections的用法
查看>>
mysql 二进制日志
查看>>
阻止putty变成inactive
查看>>
TP框架代码学习 学习记录 3.2.3
查看>>
doc文档生成带目录的pdf文件方法
查看>>
js数组,在遍历中删除元素(用 for (var i in arr)是无效的 )
查看>>
通过前端上传图片等文件的方法
查看>>
在 OC 中调用 Swift 代码
查看>>
安卓|五大逆向软件下载
查看>>
5 OK6410裸机调试(不用Jlink)
查看>>
“模板”学习笔记(5)-----编译器在处理函数模板的时候都干了啥
查看>>
教你用shell写CGI程序
查看>>
窗口 对话框 Pop Dialog 示例
查看>>
ubuntu(centos) server安装vmware tools
查看>>
数据结构之最大不重复串
查看>>
为什么要配置sdk-tools/platform-toools?
查看>>
自己动手开发更好用的markdown编辑器-07(扩展语法)
查看>>
队列的循环队列
查看>>
程序中的日期格式
查看>>