IOS UIPickerView选择器的使用

UIPickerView的基本概念

它是一个列选择器,可以设置列数和每一列的行数,然后通过调用它的代理方法设置每一个部分的尺寸参数和内容参数。并且可以设置它被选择后的触发效果。

源码分析

成员变量:

//此处有两个可选择的协议,实现协议后可以调用相应的代理方法
@protocol UIPickerViewDataSource, UIPickerViewDelegate;
//继承自uiview
NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIPickerView : UIView <NSCoding>
//数据源代理
@property(nullable,nonatomic,weak) id<UIPickerViewDataSource> dataSource;                // default is nil. weak reference
//代理
@property(nullable,nonatomic,weak) id<UIPickerViewDelegate>   delegate;                  // default is nil. weak reference
//显示选中框(设置与否区别不大)
@property(nonatomic)        BOOL                       showsSelectionIndicator;   // default is NO
// info that was fetched and cached from the data source and delegate
//组件数,也就是列数
@property(nonatomic,readonly) NSInteger numberOfComponents;

成员方法:

//这两个方法用于设置行数和行尺寸
- (NSInteger)numberOfRowsInComponent:(NSInteger)component;
- (CGSize)rowSizeForComponent:(NSInteger)component;

// returns the view provided by the delegate via pickerView:viewForRow:forComponent:reusingView:
// or nil if the row/component is not visible or the delegate does not implement 
// pickerView:viewForRow:forComponent:reusingView:
//获取指定行列对应的视图,默认返回的是nil
- (nullable UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component;

// Reloading whole view or single component
//重载组件(数据)
- (void)reloadAllComponents;
//指定组件重载
- (void)reloadComponent:(NSInteger)component;

// selection. in this case, it means showing the appropriate row in the middle
//某个行列被选中
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;  // scrolls the specified row to center.
//某个组件被选中的行数
- (NSInteger)selectedRowInComponent:(NSInteger)component; 

协议方法:
此协议内容必须被实现:

@protocol UIPickerViewDataSource<NSObject>
@required

// returns the number of 'columns' to display.
//设置列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;

// returns the # of rows in each component..
//设置行数
- (NSInteger)pickerView:(UIPickerV
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值