PickerView的相关属性用法

来源:互联网 发布:电磁炉和电陶炉 知乎 编辑:程序博客网 时间:2024/06/02 02:06
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>

@property (weak, nonatomic) IBOutlet UIPickerView *picker;

@end
-----------------------------------------------------------------------------------------------------------------
#import "ViewController.h"

@interface ViewController ()
{
    // 第一列的数据源
    NSArray *_colOne;
    // 第二列的数据源
    NSArray *_colTwo;
}
@end

@implementation ViewController
/*
 在指定PickerView的数据源之前,它是不会显示的
 一旦通过PickView的数据源方法指定了数据源,PickerView就可以显示了
 */

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 定义选择器的数据源
    _colOne = @[@"a", @"b", @"c"];
    _colTwo = @[@"1", @"2", @"3", @"4", @"5"];
}

#pragma mark - PickerView 数据源方法
// 选择器中的列数(Component)
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

// 每一列(Component)的行数(row)
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return _colOne.count;
    } else {
        return _colTwo.count;
    }
}

#pragma mark - PickerView 代理方法
// 设置component列row行显示的字符串内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0) {
        return _colOne[row];
    } else {
        return _colTwo[row];
    }
}

// 选中第component列row行的内容
// 参数说明:
// component:当前选中的列
// row:当前选中的行
// 从参数上看,我们不能直接知道所有列和行的选中信息
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//    NSString *leftCol;
//    NSString *rightCol;
//
//    if (component == 0) {
//        // 1. 获取用户当前选中的内容,就是用户用鼠标点的项
//        leftCol = _colOne[row];
//        
//        // 取出第二列选中的行数
//        // 通过指定列数,可以获取到其他列当前选中的行数
//        // 其他列中的选择情况,需要用代码去获取,不是通过代理方法传递的
//        
//        // 2. 找第二列当前选中的行数
//        NSInteger rightRow = [pickerView selectedRowInComponent:1];
//        NSLog(@"第二列选中的行数是 %d", rightRow);
//        // 3. 获取第二列当前选中行的内容
//        rightCol = _colTwo[rightRow];
//    } else {
//        // 1. 获取用户当前在第二列选中的内容
//        rightCol = _colTwo[row];
//        
//        // 2. 找到第一列当前选中的行数
//        NSInteger leftRow = [pickerView selectedRowInComponent:0];
//        NSLog(@"第一列选中的行数是 %d", leftRow);
//        // 3. 获取第一列当前选中行的内容
//        leftCol = _colOne[leftRow];
//    }
    
    NSLog(@"第一列选中的行数是 %d,第二列选中的行数是 %d", [pickerView selectedRowInComponent:0], [pickerView selectedRowInComponent:1]);
    
    NSInteger leftCol = [pickerView selectedRowInComponent:0];
    NSInteger rightCol = [pickerView selectedRowInComponent:1];
    
    NSLog(@"%@ ~~~ %@", _colOne[leftCol], _colTwo[rightCol]);
}

@end
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 包裹发出不想要了怎么办 淘宝改登录密码忘记了怎么办 三星s8应用闪退怎么办 淘宝购物商家不发货怎么办 淘宝商家迟迟不发货怎么办 买家地址写错了怎么办 买家写错快递地址怎么办 淘宝退款选择服务类型出不来怎么办 网购东西发错了怎么办 拒签商家不退款怎么办 红米手机无响应怎么办 淘宝网登录密码忘记了怎么办 淘宝网密码忘记了怎么办 京东抢购不发货怎么办 微商不想做了怎么办 减肥过程中饿了怎么办 滴滴车龄超过6年怎么办 网络公选课没过怎么办 大学网络课挂了怎么办 淘宝虚拟订单买家恶意退款怎么办 淘宝卖家虚拟发货怎么办 淘宝买虚拟产品被骗了怎么办 哈尔滨暖气低于十八度怎么办 淘客店铺没人买怎么办 淘宝商家不给退货怎么办 淘宝卖家拒绝退款申请怎么办 运费险赔付少了怎么办 买了运费险退货怎么办 卖家运费险退货怎么办 京东生鲜有坏的怎么办 与上级意见不一致时你将怎么办 物金所倒闭投资怎么办 电商平台欺骗客户怎么办 pdf电脑打开是乱码怎么办 excel表格打开是乱码怎么办 win10安装软件出现乱码怎么办 华为手机速度越来越慢怎么办 oppo手机速度越来越慢怎么办 安卓手机速度越来越慢怎么办 青桔单车忘了锁怎么办 华为手机反应太慢了怎么办