UIPickerView常用方法详解

- (NSInteger) numberOfRowsInComponent:(NSInteger)component

参数为component的序号(从左到右,以0起始),返回指定的component中row的个数

-(void) reloadAllComponents

调用此方法使得PickerView向delegate: Query for new data for all components

-(void) reloadComponent: (NSInteger) component

参数为需更新的component的序号,调用此方法使得PickerView向其delegate: Query for new data

-(CGSize) rowSizeForComponent: (NSInteger) component

参数为component的序号,返回值为the size of rows in the given components, picker view 通过调用委托方法中的pickerView:widthForComponent:和pickerView:rowHeightForComponent:获得返回值

-(NSInteger) selectedRowInComponent: (NSInteger) component

参数为component的序号,返回被选中row的序号,若无row被选中,则返回-1

-(void) selectRow: (NSInteger)row inComponent: (NSInteger)component animated: (BOOL)animated

作用:在代码指定要选择的某component的某row

参数:row序号,component序号,BOOL值(若为YES,转动spin到你选择的新值,若为NO,直接显示你选择的值)

-(UIView *) viewForRow: (NSInteger)row forComponent: (NSInteger)component

参数:row序号,component序号,返回由委托方法pickerView:viewForRow:forComponentreusingView:指定的view.如果委托对象并没有实现这个方法,或者说这个view并不是可见的,则返回nil

UIPickerViewDelegate中的实例方法

-(void) pickerView: (UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent: (NSInteger)component

作用: 当用户选择某个row时,picker view调用此函数

参数: pickerView representing the picker view request the data

-(CGFloat) pickerView:(UIPickerView *)pickerView rowHeightForComponent: (NSInteger) component

作用:由picker view调用,当其在绘制row内容,需要row的高度时

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger) component

作用: 当picker view需要给指定的component.row指定title时,调用此函数

-(UIView *)pickerView: (UIPickerView *)pickerView view ForRow:(NSInteger) row forComponent:(NSInteger) component reusingView:(UIView *) view

作用: 当picker view需要给指定的component.row指定view时,调用此函数.返回值为用作row内容的view

参数: view参数, a view object that was previously used for this rows, but is now hidden and cached by the picker view

- (CGFloat)pickerView: (UIPickerView *)pickerView widthForComponent:(NSInteger) component

作用:当picker view 需要row的宽度时,调用此函数

UIPickerViewDataSource中的实例方法

按照官方文档的说法,UIPickerViewDataSource这个协议仅有的功能就是提供picker view中component的个数和各个component中的row的个数,虽然名为datasource,但是它工作于MVC的C中

本协议仅有两个实例方法,均需要实现

-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView

作用:返回pickerView应该有几个component

-(NSInteger) pickerView:(UIPickerView *) pickerView numberOfRowsInComponent:(NSInteger) component

作用:返回指定component应该有几个row

原文地址:https://www.cnblogs.com/AbelChen1991/p/3790379.html