iOS--UIPickerView学习

UIPickerView

使用场景和功能

UIPickerView 最常见的用途是作为选项选择器,允许用户从多个选项中选择一个。UIPickerView 可以用于以滚动的方式展示大量数据,并允许用户通过滚动选择感兴趣的数据。一种很常见的用法是把各个数据以滚动的形式组成一个组合数据,比如年-月-日的日历或者时钟,以及省级-市级-区的地域 ;

这一部分的学习是参考学长的,用到了学长的plist文件 ;

UIPickerView

这个控件是iOS中自带的控件,使用方法类似于UItableview,所以就从UItableveiw的使用来分析了,这里跟着别人做了一个省级-市级-区的地域的小demo

遵循代理协议和数据源协议

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

@interface ViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, strong) UIPickerView* pickerview ;
@property (nonatomic, strong) NSMutableArray* provinceArray ;
@property (nonatomic, strong) NSArray* cityArray ;
@property (nonatomic, strong) NSArray* areaArray ;
@end

创建对象,添加代理

objectivec 复制代码
self.pickerview = [[UIPickerView alloc] initWithFrame:CGRectMake(50, 100, 300, 200)] ;
    //类似于tableview,需要设置代理和数据源
    self.pickerview.delegate = self ;
    self.pickerview.dataSource = self ;
    [self.view addSubview:self.pickerview] ;

必须实现的代理方法

objectivec 复制代码
//组数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}
//行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return [self.provinceArray count];
    } else if (component == 1) {
        return [self.cityArray count];
    } else {
        return [self.areaArray count] ;
    }
}

可以参考Uitableview的行和列,差不多的

非必要实现的方法

objectivec 复制代码
//展示的信息,可以是NSSTring,也可以是UIview
//- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
//    return @"chabuduo";
//}
//

//pickerview滑动函数

  • (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

简单常用的上面两个,还有很多其他的代理方法,可以参考UItableview;

UIPickerView 的代理方法用于处理用户的选择操作、自定义选项的外观以及其他与 UIPickerView 相关的事件。以下是一些常用的 UIPickerViewDelegate 和 UIPickerViewDataSource 协议中的代理方法:

UIPickerViewDelegate 协议中的代理方法:

pickerView(:didSelectRow:inComponent:):
当用户选择了 UIPickerView 中的某一行时调用该方法。你可以在该方法中获取选择的行和列索引,并根据需要进行相应的处理。
pickerView(
:viewForRow:forComponent:reusing:):

返回自定义 UIView 对象作为指定行和列的内容视图。你可以使用此方法自定义选项的外观,例如改变文本颜色、字体等。

pickerView(:rowHeightForComponent:):
返回指定列的行高。你可以使用此方法设置特定列的行高,以使选项在 UIPickerView 中正确显示。
UIPickerViewDataSource 协议中的代理方法:
numberOfComponents(in:):
返回 UIPickerView 中的列数(组件数)。你需要在此方法中指定需要显示的列数。
pickerView(
:numberOfRowsInComponent:):

返回指定列中的行数。你需要在此方法中指定每个列的行数,以确定每列将显示多少选项。

demo用到的其他函数

objectivec 复制代码
- (void)loadData {
    self.provinceArray = [[NSMutableArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"area.plist" ofType:nil]] ;
    self.cityArray = [[self.provinceArray objectAtIndex:0] objectForKey:@"cities"] ;
    self.areaArray = [[self.cityArray objectAtIndex:0] objectForKey:@"areas"] ;
}
objectivec 复制代码
- (UIView*)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel* myview = nil ;
    myview = [[UILabel alloc] init] ;
    myview.textAlignment = NSTextAlignmentCenter ;
    myview.font = [UIFont systemFontOfSize:22] ;
    if (component == 0) {
        myview.text = [[self.provinceArray objectAtIndex:row] objectForKey:@"state"] ;
    } else if (component == 1) {
        myview.text = [[self.cityArray objectAtIndex:row] objectForKey:@"city"] ;
    } else {
        myview.text = [self.areaArray objectAtIndex:row] ;
    }
    return myview ;
}
objectivec 复制代码
//pickerview滑动函数
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        self.cityArray = [[self.provinceArray objectAtIndex:row] objectForKey:@"cities"] ;
        [self.pickerview reloadComponent:1] ;
        //调整各组所选的对象
        [self.pickerview selectRow:0 inComponent:1 animated:YES] ;
        if ([self.cityArray count] != 0) {
            self.areaArray = [[self.cityArray objectAtIndex:0] objectForKey:@"areas"] ;
            [self.pickerview reloadComponent:2] ;
            [self.pickerview selectRow:0 inComponent:2 animated:YES] ;
        }
    } else if (component == 1) {
        if ([self.cityArray count] != 0) {
            self.areaArray = [[self.cityArray objectAtIndex:row] objectForKey:@"areas"] ;
            [self.pickerview reloadComponent:2] ;
            [self.pickerview selectRow:0 inComponent:2 animated:YES] ;
        }
    } else {
        
    }
}

提示

其他没啥好说的了,因为用起来真的太像UItableview了,这里简单解释一下滑动函数的逻辑,如果滑动"省",则更新市和区的component并其row归零;如果滑动"市",则更新区的component并归零 ;

效果展示

相关推荐
茶底世界之下1 小时前
Metal 冷启动并发下,Pipeline Cache 为什么必须合并在途编译
ios·swift
inferno1 小时前
SVN学习笔记(二)
笔记·学习·svn
传奇开心果编程1 小时前
【ArkUI进阶练中学】第12课:应用架构演进与遗留系统迁移
学习·ui·华为·harmonyos
我想我不够好。1 小时前
装一个纯净的系统
学习
青柠之夏cc2 小时前
Kotlin Multiplatform:一次编写,全平台运行,2026 年跨平台开发的 “最优解”?
macos·objective-c·cocoa
weixin_403810132 小时前
用 Python 调 EasyClick iOS 免越狱脚本:开放接口对接步骤与参数
ios·iphone
小黄蚁2 小时前
使用LVGL模拟示波器创建正弦函数波形
单片机·学习
辣知2 小时前
辣知·化智71 四川广汉三星堆的金铜贝
学习
天天进步20152 小时前
90天AI接单学习路线:从LLM应用到AI Agent
人工智能·学习
vx_Biye_Design3 小时前
springboot小学生英语学习APP62773-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·python·学习·课程设计