iOS开发之基础视图—— UIPickerView

    UIPickerView组件类似HTML都Select组件效果,提供数据供用户选择。可以通过plist文件提供数据。UIPickerView是一个选择器控件,可以生成单列的选择器,也可以生成多列的选择器,而且开发者完全可以自定义选择项的外观,用法十分灵活。UIPickerView直接继承了UIView,没有继承UIControl,因此,它不能像UIControl那样绑定事件处理方法,UIPickerView的事件处理由其委托对象完成。


    例子一——单列选择器

//
//  ViewController.m
//  UIPickerViewDemo
//
//  Created by Apple on 16/5/17.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

NSArray* timors;

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建、并初始化NSArray对象。
    timors = [NSArray arrayWithObjects:@"提莫1",
             @"提莫2", @"提莫3" , @"提莫4",@"提莫5",nil];
    
    UIPickerView* pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
    // 为UIPickerView控件设置dataSource和delegate
    [pickerView setDataSource:self];
    [pickerView setDelegate:self];
    // 设置默认选中的值
    // selectRow 表示下拉列表的默认值(从0开始)
    // inComponent 表示第几个下拉列表对象 (从0开始)
    // 注意:数据初始化要在下面的代码之前完成 否则数据显示不了
    [pickerView selectRow:1 inComponent:0 animated:YES];
    
    [self.view addSubview:pickerView];
    
}

#pragma mark -UIPickerViewDataSource

// UIPickerViewDataSource中定义的方法,设置出现几个下拉列表 (返回2 表示出现2个下拉列表)
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
    return 1;
}
// UIPickerViewDataSource中定义的方法,该方法返回值决定该控件指定列包含多少个列表项
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
    // 由于该控件只包含一列,因此无需理会列序号参数component
    // 该方法返回timors.count,表明books包含多少个元素,该控件就包含多少行
    return timors.count;
}

// 当用户选中UIPickerViewDataSource中指定列、指定列表项时激发该方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component
{
    // 使用一个UIAlertView来显示用户选中的列表项
    UIAlertView* alert = [[UIAlertView alloc]
                          initWithTitle:@"提示"
                          message:[NSString stringWithFormat:@"你选中的提莫是:%@"
                                   , [timors objectAtIndex:row]]
                          delegate:nil
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:nil];
    [alert show];
}

#pragma mark -UIPickerViewDelegate
// UIPickerViewDelegate中定义的方法,该方法返回的NSString将作为UIPickerView
// 中指定列、指定列表项的标题文本
- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    // 由于该控件只包含一列,因此无需理会列序号参数component
    // 该方法根据row参数来返回timors中的元素,row参数代表列表项的编号,
    // 因此该方法表示第几个列表项,就使用timors中的第几个元素
    return [timors objectAtIndex:row];
}

@end

    效果图如下:




    例子二——多列选择器

//
//  ViewController.m
//  MultPickerViewDemo
//
//  Created by Apple on 16/5/17.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

NSArray* heroes;
NSArray* timors;

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建、并初始化2个NSArray对象,分别作为2列的数据
    heroes = [NSArray arrayWithObjects:@"李青", @"提莫" , nil];
    timors = [NSArray arrayWithObjects: @"提莫1",
             @"提莫2", @"提莫3" , @"提莫4",@"提莫5", nil];
    
    UIPickerView* pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
    // 为UIPickerView控件设置dataSource和delegate
    [pickerView setDataSource:self];
    [pickerView setDelegate:self];
    
    [self.view addSubview:pickerView];
    
}

#pragma mark -UIPickerViewDataSource

// UIPickerViewDataSource中定义的方法,设置出现几个下拉列表 (返回2 表示出现2个下拉列表)
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
    return 2;
}
// UIPickerViewDataSource中定义的方法 设置下拉列表的数量
// 参数1:操作的UIPickerView对象
// 参数2:操作的UIPickerView对象的下拉列表的下标(从0开始)
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
    // 如果是第一列,返回authors中元素的个数
    // 即authors包含多少个元素,第一列就包含多少个列表项
    if (component == 0) {
        return heroes.count;
    }
    // 如果是其他列,返回books中元素的个数。
    // 即books包含多少个元素,其他列(只有第二列)包含多少个列表项
    return timors.count;
}

// 当用户选中UIPickerViewDataSource中指定列、指定列表项时激发该方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component
{
    NSArray* tmp  = component == 0 ? heroes: timors;
    NSString* tip = component == 0 ? @"英雄": @"提莫";
    // 使用一个UIAlertView来显示用户选中的列表项
    UIAlertView* alert = [[UIAlertView alloc]
                          initWithTitle:@"提示"
                          message:[NSString stringWithFormat:@"你选中的%@是:%@,"
                                   , tip , [tmp objectAtIndex:row]]
                          delegate:nil
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:nil];
    [alert show];
}

#pragma mark -UIPickerViewDelegate
// UIPickerViewDelegate中定义的方法 设置下拉列表的数据
// 参数1:操作的UIPickerView对象
// 参数2:下拉列表项下标(从0开始)[下拉列表的第几项]
// 参数3:操作的UIPickerView对象的下拉列表的下标[第几个下拉列表](从0开始)
- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    // 如果是第一列,返回heroes中row索引处的元素
    // 即第一列的列表项标题由heroes集合元素决定。
    if (component == 0) {
        return [heroes objectAtIndex:row];
    }else{
        // 如果是其他列(只有第二列),返回timors中row索引处的元素
        // 即第二列的列表项标题由books集合元素决定。
        return [timors objectAtIndex:row];
    }
}

// UIPickerViewDelegate中定义的方法,该方法返回的NSString将作为
// UIPickerView中指定列的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView
    widthForComponent:(NSInteger)component
{
    // 如果是第一列,宽度为90
    if (component == 0) {
        return 90;
    }
    // 如果是其他列(只有第二列),宽度为210
    return 210;
}


@end

    效果图如下:



    例子3-相互依赖的多列选择器

//
//  ViewController.m
//  MultPickerView2Demo
//
//  Created by Apple on 16/5/17.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

NSArray* heroes;
NSDictionary* skins;
// selectedHero保存当前选中的英雄
NSString* selectedHero;

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建、并初始化NSDictionary对象。
    skins = [NSDictionary dictionaryWithObjectsAndKeys:
             [NSArray arrayWithObjects:@"李青1" , @"李青2", nil]
             , @"李青",
             [NSArray arrayWithObjects:@"提莫1",@"提莫2", nil] , @"提莫",
             [NSArray arrayWithObjects:@"瑞文1",
              @"瑞文2", @"瑞文3" ,  nil]
             , @"瑞文" ,nil];
    // 使用heroes保存skins所有key组成的NSArray排序后的结果
    heroes = [skins allKeys];
    // 设置默认选中的作者heroes中第一个元素
    selectedHero = [heroes objectAtIndex:0];
    
    UIPickerView* pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
    // 为UIPickerView控件设置dataSource和delegate
    [pickerView setDataSource:self];
    [pickerView setDelegate:self];
    
    [self.view addSubview:pickerView];
    
}

#pragma mark -UIPickerViewDataSource

// UIPickerViewDataSource中定义的方法,设置出现几个下拉列表 (返回2 表示出现2个下拉列表)
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
    return 2;
}
// UIPickerViewDataSource中定义的方法 设置下拉列表的数量
// 参数1:操作的UIPickerView对象
// 参数2:操作的UIPickerView对象的下拉列表的下标(从0开始)
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
    // 如果是第一列,返回heroes中元素的个数
    // 即heroes包含多少个元素,第一列就包含多少个列表项
    if (component == 0) {
        return heroes.count;
    }
    // 如果是其他列(只有第二列),
    // 返回skins中selectedHero对应的NSArray中元素的个数。
    return [[skins objectForKey:selectedHero] count];
}

// 当用户选中UIPickerViewDataSource中指定列、指定列表项时激发该方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component
{
    if(component == 0)
    {
        // 改变被选中的英雄
        selectedHero = [heroes objectAtIndex:row];
        // 重点在这, 选中上层时, 相应的下层的数据会变动, 此时需要reloadComponent
        // 控制重写加载第二个列表,根据选中的作者来加载第二个列表
        [pickerView reloadComponent:1];
    }
    NSArray* tmp  = component == 0 ? heroes:
    [skins objectForKey:selectedHero];
    NSString* tip = component == 0 ? @"英雄": @"皮肤";
    // 使用一个UIAlertView来显示用户选中的列表项
    UIAlertView* alert = [[UIAlertView alloc]
                          initWithTitle:@"提示"
                          message:[NSString stringWithFormat:@"你选中的%@是:%@,"
                                   , tip , [tmp objectAtIndex:row]]
                          delegate:nil
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:nil];
    [alert show];
}

#pragma mark -UIPickerViewDelegate
// UIPickerViewDelegate中定义的方法 设置下拉列表的数据
// 参数1:操作的UIPickerView对象
// 参数2:下拉列表项下标(从0开始)[下拉列表的第几项]
// 参数3:操作的UIPickerView对象的下拉列表的下标[第几个下拉列表](从0开始)
- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    // 如果是第一列,返回heroes中row索引处的元素
    // 即第一列的元素由heroes集合元素决定。
    if (component == 0) {
        return [heroes objectAtIndex:row];
    }
    // 如果是其他列(只有第二列),
    // 返回skins中selectedHero对应的NSArray中row索引处的元素
    return [[skins objectForKey:selectedHero] objectAtIndex:row];
}

// UIPickerViewDelegate中定义的方法,该方法返回的NSString将作为
// UIPickerView中指定列的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView
    widthForComponent:(NSInteger)component
{
    // 如果是第一列,宽度为90
    if (component == 0) {
        return 90;
    }
    // 如果是其他列(只有第二列),宽度为210
    return 210;
}



@end

    效果图如下:


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要自定义一个 UIPickerView,你需要遵循 UIPickerViewDataSource 和 UIPickerViewDelegate 协议,并实现其中的方法。 首先,你需要创建一个 UIPickerView 实例,并设置其数据源和代理为当前视图控制器。 ```swift class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate { let pickerView = UIPickerView() override func viewDidLoad() { super.viewDidLoad() pickerView.dataSource = self pickerView.delegate = self view.addSubview(pickerView) } } ``` 接着,你需要实现 UIPickerViewDataSource 协议中的两个必须方法:numberOfComponents(in:) 和 numberOfRows(inComponent:)。前者用于返回选择器中的列数,后者用于返回每列中的行数。 ```swift func numberOfComponents(in pickerView: UIPickerView) -> Int { return 2 } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return 10 } ``` 这里我们设置了两列,每列都有 10 行。 然后,你需要实现 UIPickerViewDelegate 协议中的方法来自定义选择器的样式和行为。比如,你可以自定义每一行的标题和宽度,以及选择器的动画效果。 ```swift func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return "Row \(row)" } func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat { return 100 } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { print("Selected row \(row) in component \(component)") } ``` 这里我们简单地返回了每一行的标题为 "Row \(row)",每列的宽度为 100,当选择器的行被选中时,会在控制台输出相应的信息。 最后,你需要将 UIPickerView 添加到你的视图中,并设置它的位置和大小。 ```swift pickerView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 200) ``` 这里我们将选择器的宽度设置为与视图宽度相同,高度为 200,然后将其添加到视图的顶部。 这样,你就可以自定义一个 UIPickerView 了!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值