1、UIPickerView常用的方法:
UIPickerView代理:
1 @property(nonatomic, assign) id<UIPickerViewDelegate> delegate; 2 @property(nonatomic, assign)id<UIPickerViewDataSource> dataSource;
delegate定义了UIPickerView的外观和属性;
dataSource定义了UIPickerView的数据源和定制内容
- (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component;
返回component列、row行的一个UIView,这里只有在定制的情况下才有效,其他情况返回nil;
- (void)reloadAllComponets;
- (void)reloadComponent:(NSInteger)component;
重新装载整个UIPickerView所有列的数据和指定列的数据;
- (void)selectROw:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;
选中UIPickerView中component列、row行,也就是让改行滚动到中央;
- (void)(NSInteger)selectedRowInComponent:(NSInteger)component;
返回指定列component中选中的行,没有选中返回-1;
- (NSInteger)numberOfComponentsInPickerView;
返回UIPickerView一共有几列;
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponet:(NSInteger)component;
返回指定的component列有几行数据;
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
返回UIPickerView中component列的宽度;
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponnet:(NSInteget)component;
返回UIPickerView中component列中每行的高度;
- (void)pickerView:(UIPickerView *) pickView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
当列component、行row选中的回调函数;
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
标准的UIPickerView内容,只有字符串;
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
自定义UIPickerView内容,给每一个行、列设置一个UIView对象。(PS:上面两个方法只能二选一)
2、UIPickerViewDelegate定制:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
自定义的UIPickerView内容,给每一个列、行设置一个UIView对象。
这里为列component、行row返回一个UIView用来显示在UIPickerView中。reusingView:(UIView *)view是iOS内核传入一个缓存的UIView。在程序中可以不用分配UIView,而可以重用iOS传过来的view。