怎样设置uipickerview默认选中自动选中某一行

[转载]UIPickerView的使用技巧之二----默认选中
为UIPickerView选择一个默认的值
呵呵,个人建议---&用显示选中结果的UIView
&的tag值记录上次选中的位置
[pickerView&selectRow:boundButton.tag&inComponent:0&animated:YES];
注:参数一选中默认值在选择器的位置的行数
参数二选中默认值在选择器的位置的组件索引
参数三是否设置过度动画
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。下次自动登录
关注移动互联网和移动APP开发工具、开发框架、测试工具、微信开发、Android源码、Android开源类库以及各种开源组件的IT科技网站
现在的位置:
IOS基础UI之(十) UIPickerView和UIDatePicker详解
UIPickerView和UIDatePicker使用起来相对比较简单,下面通过简单例子深入掌握它们。
UIPickerView
1.UIPickerView 属性
// 数据源(用来告诉UIPickerView有多少列多少行)
@property(nonatomic,assign) id dataS
// 代理(用来告诉UIPickerView每1列的每1行显示什么内容,监听UIPickerView的选择)
@property(nonatomic,assign) id
// 是否要显示选中的指示器
@property(nonatomic)
showsSelectionI
// 一共有多少列
@property(nonatomic,readonly) NSInteger numberOfC
2.UIPickerView方法
// 重新刷新所有列
- (void)reloadAllC
// 重新刷新第component列
- (void)reloadComponent:(NSInteger)
// 主动选中第component列的第row行
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)
// 获得第component列的当前选中的行号
- (NSInteger)selectedRowInComponent:(NSInteger)
3.UIPickerView数据源方法
一共有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerV
第component列一共有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)
4.UIPickerView代理方法
第component列的宽度是多少
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)
第component列的行高是多少
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)
第component列第row行显示什么文字
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)
第component列第row行显示怎样的view(内容)
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)
选中了pickerView的第component列第row行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)
UIPickerView例子(联动效果)
1.创建UIPickerView,设置代理和数据源,添加到view
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,64,320, 200)];
pickerView.dataSource =
pickerView.delegate =
[self.view addSubview:pickerView];
2.懒加载省市数据
@property (nonatomic,strong) NSArray *
-(NSArray *)provinces{
if (_provinces == nil) {
NSMutableArray *provincesArr = [NSMutableArray array];
NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@cities.plist ofType:nil]];
for (NSDictionary *dict in arr) {
ZXHProvince *province = [ZXHProvince provinceWithDict:dict];
[provincesArr addObject:province];
_provinces = provincesA
3.实现数据源方法。返回多少列,每一列多少行
#pragma mark 数据源
//返回多少列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
NSLog(@==数据源==numberOfComponentsInPickerView:);
//返回每一列多少行
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
NSLog(@==数据源===numberOfRowsInComponent);
if (component==0) {//省份
return self.provinces.
}else{//市
//获得选中了哪一个省
NSInteger index = [pickerView selectedRowInComponent:0];
ZXHProvince *province = self.provinces[index];
NSArray *cities = province.
return cities.
4.实现代理方法。注意:监听省份的时候要刷新第二列城市,重新设置城市数据
#pragma mark 代理方法
//显示的数据
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSLog(@== 代理方法===titleForRow=====%ld,row);
if (component==0) {
ZXHProvince *province = self.provinces[row];
return province.
//获得选中了哪一个省
NSInteger index = [pickerView selectedRowInComponent:0];
ZXHProvince *province = self.provinces[index];
return province.cities[row];
* 监听选中了某一列的某一行
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (component==0) {//省份
// 刷新第1列的数据(重新刷新数据,重新调用数据源和代理的相应方法获得数据)
[pickerView reloadComponent:1];
//默认显示
[pickerView selectRow:0 inComponent:1 animated:YES];
//更改显示文字
//选择省份
ZXHProvince *
NSInteger pIndex = [pickerView selectedRowInComponent:0];
province = self.provinces[pIndex];
NSString *name = province.
NSInteger cIndex = [pickerView selectedRowInComponent:1];
NSString *city = province.cities[cIndex];
self.showTextLable.text = [NSString stringWithFormat:@省份:%@
城市:%@,name,city];
5.模型数据代码(略)
UIPickerView 自定义view
既可显示文字组也可显示自定义view。当要在行中显示view,则实现一下代理方法,返回自定义view。
#pragma mark 代理方法
第component列的第row行显示怎样的view
每当有一行内容出现在视野范围内,就会调用(调用频率高) 不用使用标识
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UIView *v = [[UIView alloc]init];
v.backgroundColor = [UIColor redColor];
v.frame =CGRectMake(0, 0,320,100);
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 20, 100,30)];
[btn setTitle:@test forState:UIControlStateNormal];
[UIButton buttonWithType:UIButtonTypeContactAdd];
[v addSubview:btn];
ZXHFlagView *flagView = [ZXHFlagView flagViewWithreusingView:view];
flagView.flag = self.flags[row];
return flagV
UIPickerView显示自定义view的效果图:
UIDatePicker
1.常见属性
// datePicker的显示模式
@property (nonatomic) UIDatePickerMode datePickerM
// 显示的区域语言
@property (nonatomic, retain) NSLocale
2.监听UIDatePicker的选择
因为UIDatePicker继承自UIControl,所以通过addTarget:...监听
3.UIDatePicker的使用
创建UIDatePicker
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
设置日期格式和语言
UIDatePickerModeTime, // Displays hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. 6 | 53 | PM)
UIDatePickerModeDate, // Displays month, day, and year depending on the locale setting (e.g. November | 15 | 2007)
UIDatePickerModeDateAndTime, // Displays date, hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. Wed Nov 15 | 6 | 53 | PM)
UIDatePickerModeCountDownTimer, // Displays hour and minute (e.g. 1 | 53)
datePicker.datePickerMode = UIDatePickerModeD
datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@zh_CN];
监听选择的日期时间
[datePicker addTarget:self action:@selector(birthdayChange:) forControlEvents:UIControlEventValueChanged];
添加到view显示
[self.view addSubview:datePicker];
UIDatePicker格式为UIDatePickerModeDate的效果:
【上篇】【下篇】UIPikerView的属性_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
UIPikerView的属性
上传于||暂无简介
阅读已结束,如果下载本文需要使用2下载券
想免费下载本文?
你可能喜欢

我要回帖

更多关于 uipickerview选中状态 的文章

 

随机推荐