UIPickView的基本使用

UIPickView和TableView一样,想要展示数据也要设置数据源和代理
设置数据源
self.pickView.dataSource = self;
设置代理
self.pickView.delegate = self;


遵守数据源,代理协议:
@interface ViewController ()
<UIPickerViewDataSource,UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *pickView;
@end


实现数据源代理方法:
总共有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView{

  return 3;
}


第component列有多少行.
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

  return 4;
}


返回每一列的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{

}


返回第一列的高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{

  return 50;
}


返回每一行的标题
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

  return @"gaowei";
}


返回每一行的视图UIView
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view{

  UIButton *btn = [UIButton
  buttonWithType:UIButtonTypeContactAdd];
  return btn;
}

posted @ 2017-05-30 16:56  iFat  阅读(264)  评论(0编辑  收藏  举报