IOS 学习---UIPickerView的使用

UIPickerView的创建并设置代理方法

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>
@end

创建

    UIPickerView *cityPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(11, 200, 350, 400)];
    cityPickerView.delegate = self;
    cityPickerView.dataSource = self;
    
    [self.view addSubview:cityPickerView];

实现UIPickerView的代理方法

UIPickerViewDelegate

//设置pickerView的列宽
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
//设置pickerView的行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;
//设置每一列每一行的标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
//自定义pickerView显示的视图
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
//设置选中行列的事件
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;

实现UIPickerView的数据源方法

UIPickerViewDataSource

//设置pickerView一共有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;

//设置pickerView每一列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;



你可能感兴趣的:(IOS 学习---UIPickerView的使用)