简单的自定义PickView

1.创建

@property (strong, nonatomic) UIPickerView *pickView;
@property (strong, nonatomic) NSArray         *dataArr;

2.初始化及简单数据源

   self.pickView = [[UIPickerView alloc] init];
   self.pickView.delegate = self;
   self.pickView.dataSource = self;
   self.sexTextField.inputView = self.pickView;
   self.dataArr = @[@"男",@"女"];

3.pickView的代理设置


#pragma mark - UIPickerViewDataSource
//有几列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
//几行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.dataArr.count;
}
//行的内容
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component __TVOS_PROHIBITED
{
    return self.dataArr[row];
}
//填充数据
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component __TVOS_PROHIBITED
{
    self.sexTextField.text = self.dataArr[row];
}

你可能感兴趣的:(简单的自定义PickView)