基础
1.uipickerview 属性
数据源(用来告诉uipickerview有多少列多少行)
@property(nonatomic,assign) id datasource;
代理(用来告诉uipickerview每1列的每1行显示什么内容,监听uipickerview的选择)
@property(nonatomic,assign) id delegate;
是否要显示选中的指示器
@property(nonatomic) bool showsselectionindicator;
一共有多少列
@property(nonatomic,readonly) nsinteger numberofcomponents;
2.uipickerview方法
重新刷新所有列
- (void)reloadallcomponents;
重新刷新第component列
- (void)reloadcomponent:(nsinteger)component;
主动选中第component列的第row行
- (void)selectrow:(nsinteger)row incomponent:(nsinteger)component animated:(bool)animated;
获得第component列的当前选中的行号
- (nsinteger)selectedrowincomponent:(nsinteger)component;
3.uipickerview数据源方法
一共有多少列
- (nsinteger)numberofcomponentsinpickerview:(uipickerview *)pickerview;
第component列一共有多少行
- (nsinteger)pickerview:(uipickerview *)pickerview numberofrowsincomponent:(nsinteger)component;
4.uipickerview代理方法
第component列的宽度是多少
- (cgfloat)pickerview:(uipickerview *)pickerview widthforcomponent:(nsinteger)component;
第component列的行高是多少
- (cgfloat)pickerview:(uipickerview *)pickerview rowheightforcomponent:(nsinteger)component;
第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;
实例
uipickerview 作为ios的一个常用控件相信大家都有这方面的需求。
今天我们就简单创建一个:
新建项目 命名:testuipickerview
在默认生成的viewcontroller中创建uipickerview
首先在viewdidload 的方法中创建
- (void)viewdidload {
[super viewdidload];
// do any additional setup after loading the view, typically from a nib.
// 选择框
uipickerview *pickerview = [[uipickerview alloc] initwithframe:cgrectmake(0, 100, 320, 216)];
// 显示选中框
pickerview.showsselectionindicator=yes;
pickerview.datasource = self;
pickerview.delegate = self;
[self.view addsubview:pickerview];
_protimelist = [[nsarray alloc]initwithobjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",nil];
_protitlelist = [[nsarray alloc]initwithobjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",nil];
}
然后,我们创建相关的代理方法
uipickerviewdatasource 相关代理
#pragma mark -- uipickerviewdatasource
// pickerview 列数
- (nsinteger)numberofcomponentsinpickerview:(uipickerview *)pickerview {
return 2;
}
// pickerview 每列个数
- (nsinteger)pickerview:(uipickerview *)pickerview numberofrowsincomponent:(nsinteger)component {
if (component == 0) {
return [_protitlelist count];
}
return [_protimelist count];
}
uipickerviewdelegate 相关代理方法
#pragma mark -- uipickerviewdelegate
// 每列宽度
- (cgfloat)pickerview:(uipickerview *)pickerview widthforcomponent:(nsinteger)component {
if (component == 1) {
return 40;
}
return 180;
}
// 返回选中的行
- (void)pickerview:(uipickerview *)pickerview didselectrow:(nsinteger)row incomponent:(nsinteger)component
{
if (component == 0) {
nsstring *_pronamestr = [_protitlelist objectatindex:row];
nslog(@"namestr=%@",_pronamestr);
} else {
nsstring *_protimestr = [_protimelist objectatindex:row];
nslog(@"_protimestr=%@",_protimestr);
}
}
//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(nsstring*)pickerview:(uipickerview *)pickerview titleforrow:(nsinteger)row forcomponent:(nsinteger)component
{
if (component == 0) {
return [_protitlelist objectatindex:row];
} else {
return [_protimelist objectatindex:row];
}
}
完成以上代码之后 我们就可以运行项目查看效果
如下图: