选择器的使用——郭挺

来源:互联网 发布:淘宝团扇 编辑:程序博客网 时间:2024/06/07 09:43

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