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: