iOS——UIPickerView选择器

UIPickerView

UIPickerView是 iOS 开发中常用的用户界面组件之一,用于在垂直方向上显示一个滚动的列表,用户可以通过滚动选择其中的一项。

UIPickerView的协议方法

UIPickerView和UItableView差不多,UIPickerView也要设置代理和数据源。UIPickerView的数据源和代理方法中:

objectivec 复制代码
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;// 设置UIPickerView的列数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;// 设置UIPickerView的行数

上面这两个方法是必须要实现的,其中前者负责设置UIPickerView的列数,后者负责设置行数。

其他协议方法:

objectivec 复制代码
// 设置PickerView第row行的选项标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; 
// 设置第component列第row行显示的视图
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
// 当选中第component列第row行的时候,就调用该方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
// 设置第component列的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
// 设置第component列的行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;

// 获取第component列第row行的视图,前提是该列必须是通过视图显示
- (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component;
// 刷新所有列的数据
- (void)reloadAllComponents;
// 刷新第component列的数据
- (void)reloadComponent:(NSInteger)component;
// 在PickerView里显示选中第component列第row的数据
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;
// 获取第component列选中的行号
- (NSInteger)selectedRowInComponent:(NSInteger)component;

注意

  1. PickerView的高度iOS9之前不能改,默认216,即使修改了也还是216;在iOS9上设置高度为0,PickerView会不显示
  2. PickerView里面每行的高度可以改
  3. 系统自带的控件,数据源和代理属性不需要IBOutlet,也能拖线。自己定义的属性,想要拖线,必须写IBOutlet。

一个demo:

ViewController.h:

objectivec 复制代码
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, copy) NSArray *myArray;
@property (nonatomic, copy) NSArray *chushiArr;
@property (nonatomic, copy) NSArray *juLiArr;
@property (nonatomic, copy) NSArray *caiPinArr;


@end

ViewController.m

objectivec 复制代码
#import "ViewController.h"

@interface ViewController () <UIPickerViewDelegate, UIPickerViewDataSource>


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pickerView = [[UIPickerView alloc] init];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    self.pickerView.frame = CGRectMake(0, 200, self.view.bounds.size.width, 500);
    self.chushiArr = @[@"销量最高", @"评分最高", @"价格最低", @"价格最高"];
    self.juLiArr = @[@"距离最近", @"距离最远"];
    self.caiPinArr = @[@"种类最多", @"销量最高", @"价格最低", @"价格最高"];
    self.myArray = self.chushiArr;
    [self.view addSubview:self.pickerView];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return 3;
    }
    return [self.myArray count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSArray *xuanZe = @[@"厨师", @"距离", @"菜品"];
    if (component == 0) {
        return xuanZe[row];
    }
    return self.myArray[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        if (row == 0) {
            self.myArray = self.chushiArr;
        } else if (row == 1) {
            self.myArray = self.juLiArr;
        } else if (row == 2) {
            self.myArray = self.caiPinArr;
        }
        [self.pickerView reloadComponent:1];
    }
}

@end

运行结果:

相关推荐
2501_928094659 分钟前
OBS - Mac专业录屏工具(Mac中文)
macos·mac·录屏
大熊猫侯佩25 分钟前
拒绝羡慕 Cursor!Xcode 自己也能利用 AI 大模型让撸码如虎添翼【超详细配置】
macos·ai编程·xcode
weixin_473894774 小时前
mac 电脑安装类似 nvm 的工具,node 版本管理工具
macos·node.js
ToTensor5 小时前
Paraformer实时语音识别中的碎碎念
人工智能·语音识别·xcode
wjm0410067 小时前
ios八股文 -- Objective-c
开发语言·ios·objective-c
飞雪200714 小时前
Alibaba Cloud Linux 3 在 Apple M 芯片 Mac 的 VMware Fusion 上部署的完整密码重置教程(二)
linux·macos·阿里云·vmware·虚拟机·aliyun·alibaba cloud
麦兜*16 小时前
Swift + Xcode 开发环境搭建终极指南
开发语言·ios·swiftui·xcode·swift·苹果vision pro·swift5.6.3
Digitally21 小时前
重置iPhone会删除所有内容吗? 详细回答
ios·iphone
普罗米拉稀1 天前
Flutter 复用艺术:Mixin 与 Abstract 的架构哲学与线性化解密
flutter·ios·面试
degree5201 天前
全平台轻量浏览器推荐|支持Win/macOS/Linux,极速加载+隐私保护+扩展插件,告别广告与数据追踪!
windows·macos·电脑