UIPickView的属性:
-
picker的高度是固定的,如果设置大于216,就是216,如果小于216大于180,就是180;如果小于180大于162,就是162。
-
设置数据源及代理(UIPickerViewDelegate,UIPickerViewDataSource)
picker.dataSource=self;
picker.delegate=self;
-
创建
UIPickerView*picker=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 50, 180, 300)];
picker.backgroundColor=[UIColor cyanColor];
[self.view addSubview:picker];
//
设置显示选中的指示条,默认为
NO
picker.showsSelectionIndicator=YES;
//
设置数据源及代理
picker.dataSource=self;
picker.delegate=self;
UIPickView的常用方法:
//
刷新整个数据源
- (void)reloadAllComponents;
//
刷新选中列的数据
- (void)reloadComponent:(NSInteger)component;
//
(用动画的效果)将指定的行滚动到中心
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;
//
返回选定的行
- (NSInteger)selectedRowInComponent:(NSInteger)component;
//
加载自定义
pickView
的行视图
****
- (nullable UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component;
UIPickView的数据源协议:
-
必须实现的协议方法:
// PickerView
有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
// PickerView
的每一列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
UIPickView的代理协议:
//
返回
picker
有多少个区
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
//
返回
picker
每个区有多少行
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return 5;
}
//
如果同时实现返回字符串和
view
的方法,返回
UIView
的优先级比较高
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view __TVOS_PROHIBITED
{
//
返回自定义
view
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
v.backgroundColor = [UIColor redColor];
return v;
}
// NSAttributedString
富文本属性
:
可以描述文字大小和颜色
(
此方法不常用
)
- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component;
//
返回
pickerView
的行标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return @"xxxx";
}
//
选中第
component
第
row
的时候调用
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}