美文网首页
UIPickerView 的使用

UIPickerView 的使用

作者: 何颀 | 来源:发表于2017-10-12 10:25 被阅读0次

控件的代码与UITabelView非常类似 更简单,代理也类似

#import "ViewController.h"

@interface ViewController ()@property (nonatomic,strong)UIPickerView * pickerV;

@property (nonatomic,strong)NSMutableArray *letter;

@property (nonatomic,strong)NSMutableArray *number;

@property (nonatomic,strong)NSMutableArray *senc;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

[self loadData];

int w =[UIScreen mainScreen].bounds.size.width;

int h = [UIScreen mainScreen].bounds.size.height;

int ph = 400;//_pickerV 的高

int py = 80;

_pickerV = [[UIPickerView alloc]initWithFrame:CGRectMake(0, py, w, ph)];

_pickerV.backgroundColor = [UIColor clearColor];

self.pickerV.delegate = self;

self.pickerV.dataSource = self;//设置代理

[self.view addSubview:self.pickerV];

UILabel * hour = [[UILabel alloc]initWithFrame:CGRectMake(w/6+20, (ph-py)/2+20, 50, 40)];//(ph-py)/2+20  20为每一 行的高度/2

hour.text = @"时";

hour.backgroundColor = [UIColor yellowColor];

[_pickerV addSubview:hour];

UILabel * fen = [[UILabel alloc]initWithFrame:CGRectMake(w/2+20, (ph-py)/2+20, 50, 40)];

fen.text = @"分";

fen.backgroundColor = [UIColor yellowColor];

[_pickerV addSubview:fen];

UILabel * miao = [[UILabel alloc]initWithFrame:CGRectMake(w/6*5+20, (ph-py)/2+20, 50, 40)];

miao.text = @"秒";

miao.backgroundColor = [UIColor yellowColor];

[_pickerV addSubview:miao];

UIButton * butt = [UIButton buttonWithType:0];

butt.frame = CGRectMake(0, 440, 100, 40);

butt.backgroundColor = [UIColor grayColor];

[butt addTarget:self action:@selector(clioi) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:butt];

[_pickerV selectRow:6 inComponent:0 animated:YES];

[_pickerV selectRow:6 inComponent:1 animated:YES];

[_pickerV selectRow:6 inComponent:2 animated:YES];

//NSString *seletedHeight = [ objectAtIndex:selectedHeightIndex];

//self.heightLabel.text = seletedHeight;

}

-(void)clioi//获取当前选择的

{

NSInteger sIndex = [self.pickerV selectedRowInComponent:0];

NSInteger eIndex = [self.pickerV selectedRowInComponent:1];

NSInteger  lIndex = [self.pickerV selectedRowInComponent:2];

NSLog(@"%zi,%zi,%zi",sIndex,eIndex,lIndex);

}

-(void)loadData

{

self.letter = [NSMutableArray new];

self.number = [NSMutableArray new];

self.senc = [NSMutableArray new];

//需要展示的数据以数组的形式保存

for (int i = 0; i<24; i++) {

[self.letter addObject:[NSString stringWithFormat:@"%d",i]];

}

for (int i = 0; i<60; i++) {

[self.number addObject:[NSString stringWithFormat:@"%d",i]];

}

for (int i = 0; i<60; i++) {

[self.senc addObject:[NSString stringWithFormat:@"%d",i]];

}

}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

return 3;//Component 数

}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component//行高

{

return 40;

}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component //行数

{

NSInteger result = 0;

switch (component) {

case 0:

result = self.letter.count;//根据数组的元素个数返回几行数据

break;

case 1:

result = self.number.count;

break;

case 2:

result = self.senc.count;

break;

default:

break;

}

return result;

}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component//行内容

{

NSString * title = nil;

switch (component) {

case 0:

title = self.letter[row];

break;

case 1:

title = self.number[row];

break;

case 2:

title = self.number[row];

break;

default:

break;

}

return title;

}

@end

相关文章

  • 36-Swift之UIPickerView

    一、UIPickerView的使用和介绍 UIPickerView是一个选择器控件,它比UIDatePicker更...

  • UI -- UIPickerView(拾取器)的使用

    一、UIPickerView(拾取器)的使用 1、UIPickerView控件生成的表格可以提供滚动的轮盘 ...

  • Swift 街道四级地址选择 封装为Framework动态库

    使用UIPickerView实现 Xcode9及以上 Swift4 支持地址反向选中UIPickerView相应行...

  • PickerView - IOS

    一、UIPickerView(拾取器)的使用 1、UIPickerView控件生成的表格可以提供滚动的轮盘, 2、...

  • iOS学习 - UIPickerView

    UIPickerView使用: 1、单个PickerView的使用 #pragma mark ===== UIPi...

  • UIPickerView使用

    最近,上课讲解了日期选择器和普通数据选择器的使用,上课讲解的案例是假期学生去向登记,该项目有一定的实际意义,可以在...

  • UIPickerView的使用

    需要引入 RxDataSources ,它提供了许多 pickerView 适配器 适配器类型:RxPickerV...

  • UIPickerView 的使用

    控件的代码与UITabelView非常类似 更简单,代理也类似 #import "ViewController.h...

  • UIPickerView的使用

    UIPickerView继承自UIView,它的用法类似UITableView,需要设置数据源代理UIPicker...

  • UIPickerView的使用

    滚动选择器。 遵守协议 ,用法类似于UITableView一样,需要设置数据源。 创建,布局 实现数据源方法 1、...

网友评论

      本文标题:UIPickerView 的使用

      本文链接:https://www.haomeiwen.com/subject/yrsnyxtx.html