且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS开发之UIPickerView

更新时间:2022-06-27 02:09:48

1、使用方法

UIPickerView使用和UITableView大致类似。首先设置ViewController为数据源,然后遵守数据源协议< UIPickerViewDataRecouce>,之后实现协议中的方法:

一共有多少列:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)

pickerView;

 

component列显示多少行:

- (NSInteger)pickerView:(UIPickerView *)pickerView

 numberOfRowsInComponent:(NSInteger)component;

 

上面只能设置UIPickerView有多少行、多少列,不能设置数据,如果要显示数据,必须要设置ViewController为UIPickerView的代理,遵守代理协议<UIPickerViewDelegate>,然后实现代理中的方法:

component列的第row行显示什么文字:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:

(NSInteger)row forComponent:(NSInteger)component;

 

component列第row行显示怎样的view(内容)

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:

(NSInteger)row forComponent:(NSInteger)component reusingView:

(UIView *)view;

 

选中了pickerView的第component列第row

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:

(NSInteger)row inComponent:(NSInteger)component