首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
其他好文
> 详细
UIPickerView实例
时间:
2014-12-11 20:56:18
阅读:
332
评论:
0
收藏:
0
[点我收藏+]
标签:
uipickerview实例
+ (
id
)cityPicker
{
return
[[
NSBundle
mainBundle
]
loadNibNamed
:
@"MJCityPicker"
owner
:
nil
options
:
nil
][
0
];
}
#pragma mark
任何对象从
xib
中创建完毕的时候都会调用一次
- (
void
)awakeFromNib
{
NSArray
*array = [
NSArray
arrayWithContentsOfFile
:[[
NSBundle
mainBundle
]
pathForResource
:
@"cities.plist"
ofType
:
nil
]];
_provinces
= [
NSMutableArray
array
];
for
(
NSDictionary
*dict
in
array) {
MJProvince
*p = [
MJProvince
provinceWithDict
:dict];
[
_provinces
addObject
:p];
}
}
#pragma mark - UIPickerView
数据源方法
- (
NSInteger
)numberOfComponentsInPickerView:(
UIPickerView
*)pickerView
{
return
2
;
}
#pragma mark
第
component
列有多少行数据
- (
NSInteger
)pickerView:(
UIPickerView
*)pickerView numberOfRowsInComponent:(
NSInteger
)component
{
if
(component ==
0
) {
//
多少个省份
return
_provinces
.
count
;
}
else
{
//
当前选中省份的行数(城市个数)
// 1.
获得选中了哪一个省
int
pIndex = [pickerView
selectedRowInComponent
:
0
];
// 2.
取出省份模型
MJProvince
*p =
_provinces
[pIndex];
// 3.
返回当前省份城市的个数
return
p.
cities
.
count
;
}
}
#pragma mark - UIPickerView
代理方法
- (
NSString
*)pickerView:(
UIPickerView
*)pickerView titleForRow:(
NSInteger
)row forComponent:(
NSInteger
)component
{
if
(component ==
0
) {
//
显示哪个省份
// 1.
取出省份模型
MJProvince
*p =
_provinces
[row];
// 2.
取出省份名称
return
p.
name
;
}
else
{
//
显示哪个城市
// 1.
获得选中了哪一个省
int
pIndex = [pickerView
selectedRowInComponent
:
0
];
// 2.
取出省份模型
MJProvince
*p =
_provinces
[pIndex];
// 3.
返回对应行的城市名称
return
p.
cities
[row];
}
}
#pragma mark
监听选中了某一列的某一行
- (
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
];
MJProvince
*p =
_provinces
[pIndex];
// 2.
获得选中的城市位置
int
cIndex = [pickerView
selectedRowInComponent
:
1
];
// 3.
通知代理
if
([
_delegate
respondsToSelector
:
@selector
(cityPicker:citySelectWithProvince:city:)]) {
[
_delegate
cityPicker
:
self
citySelectWithProvince
:p.
name
city
:p.
cities
[cIndex]];
}
}
#pragma mark
代理协议
@protocol
MJCityPickerDelegate <
NSObject
>
@optional
- (
void
)cityPicker:(
MJCityPicker
*)cityPicker citySelectWithProvince:(
NSString
*)province city:(
NSString
*)city;
@end
注意:
由于UIPickerView
继承自
UIView
,里面有数据源和代理,因此
UIPickerView
展示数据通过数据源,监听事件通过代理。
UIPickerView实例
标签:
uipickerview实例
原文地址:http://blog.csdn.net/itcontend/article/details/41870959
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
分布式事务
2021-07-29
OpenStack云平台命令行登录账户
2021-07-29
getLastRowNum()与getLastCellNum()/getPhysicalNumberOfRows()与getPhysicalNumberOfCells()
2021-07-29
【K8s概念】CSI 卷克隆
2021-07-29
vue3.0使用ant-design-vue进行按需加载原来这么简单
2021-07-29
stack栈
2021-07-29
抽奖动画 - 大转盘抽奖
2021-07-29
PPT写作技巧
2021-07-29
003-核心技术-IO模型-NIO-基于NIO群聊示例
2021-07-29
Bootstrap组件2
2021-07-29
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!