选择器的使用——郭挺

UIPickerView和UIDataPicker都是选择器,它们的外貌也非常相似,但它们却继承自不同的父类。
1.UIPickerView继承自UIView
2.UIDataPicker继承自UIcontrol,而UIcontrol也继承自UIView
UIPickerView直接继承了UIView,没有继承UIControl,因此,它不能像UIControl那样绑定事件处理方法,UIPickerView的事件处理由其委托对象完成。
下面是uipickerview的一些属性和创建shiyong

//宽和高是固定的
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 480-216, 0, 0)];
//设置数据源和委托
pickerView.dataSource = self;
pickerView.delegate = self;
//是否显示指示器
pickerView.showsSelectionIndicator = YES; //默认为yes
[self.view addSubview:pickerView];

数据源方法,必须实现

// 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 5;//[_data count];
}
//返回第一列选择的行的索引
}

代理方法

//返回每个component中row的title
- (nullable NSString )pickerView:(UIPickerView )pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
}
//返回每个component中row的View
- (UIView )pickerView:(UIPickerView )pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view
{ }
}
//选择某一行调用该方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

}

// returns width of column and height of row for each component.
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{

}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 50;
}

附:对于单列选择器,只要控制UIPickerView的dataSource对象的numberOfComponentsInPickerView:方法返回1即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值