PickerView的用法

来源:互联网 发布:网络买彩票会坐牢吗 编辑:程序博客网 时间:2024/05/29 16:41

//加载pickerview在viewDidLoad函数中调用该函数即可,刚开始将pickerview放在屏幕最底方,通过调用下面的showPickerView函数使其从底部动态出现

-(void) addPickerView {

if (pickerView ==nil) {

pickerView = [[UIPickerViewallocinitWithFrame:CGRectMake(0,460320460)];

pickerView.delegate = self;

pickerView.dataSource = self;

pickerView.showsSelectionIndicator = YES;//选中某行时会和其他行显示不同

[self.viewaddSubview:pickerView];

[pickerViewrelease];

}

}


//使pickerview从底部出现

-(void) showPickerView {

[UIViewbeginAnimations@"Animation"context:nil];//设置动画

[UIViewsetAnimationDuration:0.3]; 

pickerView.frame =CGRectMake(0,240320460);

[UIViewcommitAnimations]; 

}


//使pickerview隐藏到屏幕底部

-(void) hidePickerView {

[UIViewbeginAnimations:@"Animation"context:nil];

[UIViewsetAnimationDuration:0.3];

pickerView.frame =CGRectMake(0,460320460);

[UIViewcommitAnimations];

}


//返回pickerview的组件数

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {    

    return 1;

}


//返回每个组件上的行数

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {    

return2;

}


//设置每行显示的内容

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

if (row ==0) {

return@"";

}else {

return@"";

}


}


//自定义pickerview使内容显示在每行的中间,默认显示在每行的左边((NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {

   UILabel *label = [[[UILabelallocinitWithFrame:CGRectMake(0.0f,0.0f, [pickerViewrowSizeForComponent:component].width, [pickerViewrowSizeForComponent:component].height)]autorelease];

if (row ==0) {

label.text =@"";

}else {

label.text =@"";

}

[labelsetTextAlignment:UITextAlignmentCenter];

    return label;

}


//当你选中pickerview的某行时会调用该函数。


- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

/NSLog(@"You select row %d",row);

if (row ==0) {

selectLabel.text = @"you select ";

}elseif (row == 1) {

selectLabel.text = @"you select ";

}*/

}


//设置指定组件上每行的宽度

/*

-(void)pickerView:(UIPickerView *)thePickerView widthForComponet:component {


}


*/


0 0
原创粉丝点击