第十二篇 - UIPickerView

初始化,设置代理

UIPickerView *picker = [[UIPickerView alloc] init];
picker.dataSource = self;
picker.delegate = self;

 

  @protocol UIPickerViewDataSource, UIPickerViewDelegate;
//
//NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIPickerView : UIView <NSCoding, UITableViewDataSource>
//
  @property(nullable,nonatomic,weak) id<UIPickerViewDataSource> dataSource;                // default is nil. weak reference
  @property(nullable,nonatomic,weak) id<UIPickerViewDelegate>   delegate;                  // default is nil. weak reference
// ios7之后设置无效
  @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:
//获取某一分区某一行的视图
  - (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;                                   // returns selected row. -1 if nothing selected
//
  @end
//
//
//__TVOS_PROHIBITED
  @protocol UIPickerViewDataSource<NSObject>
  @required
//
//// 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;
  @end
//
//__TVOS_PROHIBITED
  @protocol UIPickerViewDelegate<NSObject>
  @optional
//
//// 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

设置某一行显示的view视图 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view __TVOS_PROHIBITED; //第三个参数,是可重用view,ios6时可用。ios7就不好用了。
    //自定义view
    KKCountryView *countryView = (KKCountryView *)view;
    
    if (countryView == nil) {
        NSLog(@"创建新的自定义view");
        countryView = [KKCountryView countryView];
    }
    
    // 2.赋值模型数据
    countryView.country = self.countrys[row];
    
    // 3.返回自定义view
    return countryView;

选中某一行时执行的回调 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component __TVOS_PROHIBITED; // @end

 

你可能感兴趣的:(第十二篇 - UIPickerView)