概述
UIPickerView,选择器控件,可以实现单列或多列选择
UIPickerView
appearance
@property(nonatomic) BOOL showsSelectionIndicator; // default is NO
注意:
ios7之前,此属性控制selection indicator显示,默认值为NO,ios7及以后,此属性不再起作用,无论设置YES或NO,selection indicator总显示
selection
// 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; // returns selected row. -1 if nothing selected
dataSource
@property(nullable,nonatomic,weak) id<UIPickerViewDataSource> dataSource; // default is nil. weak reference
component&row
view
// info that was fetched and cached from the data source
@property(nonatomic,readonly) NSInteger numberOfComponents;
- (NSInteger)numberOfRowsInComponent:(NSInteger)component;
dataSource
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
注意:
- view获取的是cache值
- dataSource定制view的component和row
delegate
@property(nullable,nonatomic,weak) id<UIPickerViewDelegate> delegate; // default is nil. weak reference
cell appearance
view
// info that was fetched and cached from the delegate
- (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:
- (nullable UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component;
delegate
// returns width of column and height of row for each component.
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component __TVOS_PROHIBITED;
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component __TVOS_PROHIBITED;
// these methods return either a plain NSString, a NSAttributedString, or a view (e.g UILabel) to display the row for the component.
// for the view versions, we cache any hidden and thus unused views and pass them back for reuse.
// If you return back a different object, the old one will be released. the view will be centered in the row rect
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component __TVOS_PROHIBITED;
- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED; // attributed title is favored if both methods are implemented
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view __TVOS_PROHIBITED;
注意:
- view获取的只是cache值
- delegate定制view的cell appearance
- delegate提供了三个版本的cell appearance:NSString,NSAttributedString,UIView,分别表示UILabel(plain),UILabel(attributed),UIView,优先级:UIView>NSAttributedString>NSString
cell select
delegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component __TVOS_PROHIBITED;
reload
// Reloading whole view or single component
- (void)reloadAllComponents;
- (void)reloadComponent:(NSInteger)component;
注意:
- view绘制前会调用一次reloadAllComponents,component从默认值0改变为自定义值
- reloadAllComponents会调用dataSource的定制component和row方法,delegate的定制cell appearance的其中一个版本,如果component值发生改变,会调用delegate的定制cell size方法,如果component值没有改变,不会调用delegate的定制cell size方法
- reloadComponent会调用dataSource的指定component的定制row方法,delegate的指定component的定制cell appearance的其中一个版本