UIPickerView 简单介绍

1.UIPickerView效果
UIPickerView 简单介绍_第1张图片

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];
       
    }
}

你可能感兴趣的:(UIPickerView 简单介绍)