数据源方法
// 返回有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
// 返回每一列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
代理方法
// 每一列的每一行显示什么数据
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
// 选中的某一列的某一行<需要手动调用>
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
//返回列宽
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
//返回行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;
普通方法
// 获取当前列有多少行
NSInteger rowCount = [self.pickerView numberOfRowsInComponent:i];
// 获取指定列当前选中第几行
NSInteger row = [self.pickerView selectedRowInComponent:i];
// 此方法不会调用代理方法去选中某一列的某一行
[self.pickerView selectRow:newRow inComponent:i animated:YES];
//重新加载数据,再次调用数据源方法和代理方法
- (void)reloadAllComponents;
- (void)reloadComponent:(NSInteger)component;
"<头文件翻译>
/*{
<头文件翻译>
#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIView.h>
#import <UIKit/UITableView.h>
#import <UIKit/UIKitDefines.h>
@protocol UIPickerViewDataSource, UIPickerViewDelegate;
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIPickerView : UIView <NSCoding, UITableViewDataSource>
// 数据源
@property(nonatomic,assign) id<UIPickerViewDataSource> dataSource; // 默认为nil,弱引用
// 代理
@property(nonatomic,assign) id<UIPickerViewDelegate> delegate; // 默认为nil,弱引用
// 显示选择标示
@property(nonatomic) BOOL showsSelectionIndicator; // 默认为NO
// 列数
@property(nonatomic,readonly) NSInteger numberOfComponents;
// component列中的行数
- (NSInteger)numberOfRowsInComponent:(NSInteger)component;
// 指定列中每一行的size
- (CGSize)rowSizeForComponent:(NSInteger)component;
// 返回由代理方法pickerView:viewForRow:forComponent:reusingView:提供的视图
// 如果没有实现该代理方法,或者component列的row行不可见,则返回nil
- (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component;
// 重新加载所有列的数据
- (void)reloadAllComponents;
// 重新加载指定列的数据
- (void)reloadComponent:(NSInteger)component;
// 选中指定component列中的row行,不会触发代理方法
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated; // 滚动指定的行到中间位置
// 返回当前选中的行,如果没有选中行,返回-1
- (NSInteger)selectedRowInComponent:(NSInteger)component;
@end
// 数据源协议,所有方法都必须实现
@protocol UIPickerViewDataSource<NSObject>
@required
// 返回要显示的列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
// 返回component列中的数据行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
@end
// 代理协议,所有方法都是可选的
@protocol UIPickerViewDelegate<NSObject>
@optional
// 返回指定列的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
// 返回指定列的行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;
// 以下三个返回指定列对应行的内容
//
// 可以是:
// 1> NSString
// 2> NSAttributedString
// 如果同时实现了以上两个方法,优先选择NSAttributedString的代理方法执行
// 3> UIView
// 可以返回自定义的视图,reusingView没有真正实现应用。
//
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0);
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
// component列中的row行被选中
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
@end
}*/
网友评论