1.UIPickerView效果
2.声明UIPickerView,声明属性
@property(nonatomic,retain)NSArray*proTimeList;
@property(nonatomic,retain)NSArray*proTitleList;
@property(nonatomic,retain)NSString*proTimeStr;
@property(nonatomic,retain)NSString*proNameStr;
UIPickerView*pickerView = [[UIPickerViewalloc]initWithFrame:CGRectMake(0,100,320,216)];
//显示选中框
pickerView.showsSelectionIndicator=YES;
pickerView.dataSource=self;
pickerView.delegate=self;
[self.viewaddSubview:pickerView];
_proTitleList = [[NSArrayalloc]initWithObjects:@"1月",@"2月",@"3月",@"4月",@"5月",@"6月",@"7月",@"8月",@"9月",@"10月",@"11月",@"12月",nil];
_proTimeList = [[NSArrayalloc]initWithObjects:@"1日",@"2日",@"3日",@"4日",@"5日",@"6日",@"7日",@"8日",@"9日",@"10日",@"11日",@"12日",@"13日",@"14日",@"15日",@"16日",@"17日",@"18日",@"19日",@"20日",@"21日",@"22日",@"23日",@"24日",@"25日",@"26日",@"27日",@"28日",@"29日",@"30日",nil];
3.签协议
<UIPickerViewDelegate,UIPickerViewDataSource>
4.实现方法
// pickerView列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView {
return 2;
}
// pickerView 每列个数
- (NSInteger)pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == 0) {
return[_proTitleListcount];
}
return [_proTimeListcount];
}
// 每列宽度
- (CGFloat)pickerView:(UIPickerView*)pickerView widthForComponent:(NSInteger)component {
if (component == 1) {
return80;
}
return 180;
}
// 返回选中的行
- (void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component == 0) {
_proNameStr = [_proTitleListobjectAtIndex:row];
} else {
_proTimeStr= [_proTimeListobjectAtIndex:row];
}
}
//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0) {
return[_proTitleListobjectAtIndex:row];
} else {
return[_proTimeListobjectAtIndex:row];
}
}