ios开发pickerview怎么自定义

 我来答
就烦条0o
2016-10-23 · 知道合伙人软件行家
就烦条0o
知道合伙人软件行家
采纳数:33315 获赞数:46492
从事多年系统运维,喜欢编写各种小程序和脚本。

向TA提问 私信TA
展开全部
一、自定义PickerView的代码

1、h文件中代码如下:
#import <UIKit/UIKit.h>

typedef void (^MyBasicBlock)(id result);

@interface YCPickerView : UIView

@property (retain, nonatomic) NSArray *arrPickerData;
@property (retain, nonatomic) UIPickerView *pickerView;

@property (nonatomic, copy) MyBasicBlock selectBlock;

- (void)popPickerView;

@end

2、m文件中代码如下:
#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
#define PICKERVIEW_HEIGHT 256

#import "YCPickerView.h"

@interface YCPickerView () <UIPickerViewDelegate,UIPickerViewDataSource>
{
NSInteger selectRow;
}

@property (retain, nonatomic) UIView *baseView;

@end

@implementation YCPickerView

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {

_baseView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-PICKERVIEW_HEIGHT, SCREEN_WIDTH, PICKERVIEW_HEIGHT)];
_baseView.backgroundColor = [UIColor orangeColor];
[self addSubview:_baseView];

UIButton *btnOK = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-50, 0, 40, 40)];
[btnOK setTitle:@"确定" forState:UIControlStateNormal];
[btnOK setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btnOK addTarget:self action:@selector(pickerViewBtnOK:) forControlEvents:UIControlEventTouchUpInside];
[_baseView addSubview:btnOK];

UIButton *btnCancel = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 40, 40)];
[btnCancel setTitle:@"取消" forState:UIControlStateNormal];
[btnCancel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btnCancel addTarget:self action:@selector(pickerViewBtnCancel:) forControlEvents:UIControlEventTouchUpInside];
[_baseView addSubview:btnCancel];

_pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, PICKERVIEW_HEIGHT-40)];
_pickerView.delegate = self;
_pickerView.dataSource = self;
_pickerView.backgroundColor = [UIColor whiteColor];
[_baseView addSubview:_pickerView];

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissPickerView)];
[self addGestureRecognizer:tapGesture];

}
return self;
}

#pragma mark - UIPickerViewDataSource

//返回多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}

//每列对应多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return _arrPickerData.count;
}

//每行显示的数据
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return _arrPickerData[row];
}

#pragma mark - UIPickerViewDelegate

//选中pickerView的某一行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
selectRow = row;
}

#pragma mark - Private Menthods

//弹出pickerView
- (void)popPickerView
{
[UIView animateWithDuration:0.5
animations:^{
self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}];

}

//取消pickerView
- (void)dismissPickerView
{
[UIView animateWithDuration:0.5
animations:^{
self.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
}];
}

//确定
- (void)pickerViewBtnOK:(id)sender
{
if (self.selectBlock) {
self.selectBlock(_arrPickerData[selectRow]);
}
[self dismissPickerView];
}

//取消
- (void)pickerViewBtnCancel:(id)sender
{
if (self.selectBlock) {
self.selectBlock(nil);
}

[self dismissPickerView];

}
@end
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式