本文介绍: iOS—— UIPickerView前言在绝大部分App中,我们都可以见到一个强大的控件——UIPickerView,它可以用来选择地区,选择时间等等,所以如何掌握UIPickerView就变得特别重要,下面简单的说一下UIPickerView的实现过程:实现过程UIPickerView的创建类似于UITableView的建立,首先,UIPickerView是需要遵循数据源和代理两个协议的,所以我们在使用前需要先添加这两个协议:@interface ViewController : UIVie
iOS—— UIPickerView
前言
在绝大部分App中,我们都可以见到一个强大的控件——UIPickerView,它可以用来选择地区,选择时间等等,所以如何掌握UIPickerView就变得特别重要,下面简单的说一下UIPickerView的实现过程:
实现过程
UIPickerView的创建类似于UITableView的建立,
首先,UIPickerView是需要遵循数据源和代理两个协议的,
所以我们在使用前需要先添加这两个协议:
@interface ViewController : UIViewController<
UIPickerViewDelegate,
UIPickerViewDataSource
>
@property (nonatomic, strong) UIPickerView* pickerView;
// 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
//行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return 10;
}
// 还有一个比较重要的方法:
// 设置选择的响应事件
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
#import "ViewController.h"
#import "ZGPickerView.h"
#define kScreenSize [UIScreen mainScreen].bounds.size
#define Width [UIScreen mainScreen].bounds.size.width
#define Height [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<ZGPickerViewDelegate>
@property (nonatomic,strong) ZGPickerView *pickerView;
@property (nonatomic, strong) NSMutableArray* pickerDataSource;
@property (nonatomic,strong) NSMutableDictionary *selectedCache;
@property (nonatomic,assign) BOOL hasSetSelectedRow;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupPickerView];
// [self initMemberVar];
// [self initSelectedCache];
}
#pragma mark - zgpickerview delegate
- (void)ZGPickerView:(ZGPickerView *)pickerView currentComponent:(NSInteger)currentComponent currentRow:(NSInteger)currentRow
{
NSLog(@"currentRow=%ld,currentComponent%ld",currentRow,currentComponent);
}
#pragma mark - method
- (void)setupPickerView{
self.title = @"Second";
self.view.backgroundColor = [UIColor whiteColor];
CGFloat width = Width;
CGFloat height = Height;
CGFloat originX = (kScreenSize.width - width)*0.5;
CGFloat originY = (kScreenSize.height - height)*0.5;
self.pickerView = [[ZGPickerView alloc] initWithFrame:CGRectMake(originX, originY, width, height)];
self.pickerView.dataArr = /*@[@"aaa",@"bbb",@"ccc",@"hhh",@"ddd",@"eee"];//*/[self getData];
self.pickerView.defaultSelectedRow = /*@[@(2)];//*/@[@(5),@(0),@(10),@(15)];
self.pickerView.pvDelegate = self;
[self.view addSubview:self.pickerView];
[self.pickerView reloadAllComponents];
}
- (NSArray*)getData{
NSMutableArray* arr = [NSMutableArray new];
NSMutableArray* agetemp = [NSMutableArray new];
for (int i = 0; i < 37; i++) {
[agetemp addObject:[NSString stringWithFormat:@"%d个月",i]];
}
for (int i = 4; i <= 120; i++) {
NSString* str = [NSString stringWithFormat:@"%d岁",i];
[agetemp addObject:str];
}
[arr addObject:agetemp];
[arr addObject:@[@"男",@"女"]];
NSMutableArray* talltemp = [NSMutableArray new];
for (int i = 10; i < 230; i++) {
NSString* str = [NSString stringWithFormat:@"%dcm",i];
[talltemp addObject:str];
}
[arr addObject:talltemp];
NSMutableArray* weighttemp = [NSMutableArray new];
for (int i = 3; i < 200; i++) {
NSString* str = [NSString stringWithFormat:@"%dkg",i];
[weighttemp addObject:str];
}
[arr addObject:weighttemp];
return [arr copy];
}
原文地址:https://blog.csdn.net/weixin_51638861/article/details/123431734
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_30982.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。