本文介绍: UIPickerView是很简单但是很实用的控件,在项目里也许会用到

写在开头

UIPickerView实现简单demo

返回UIPickerView中Component列的宽度
 - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
 
 返回UIPickerView中Component列中每行高度
 - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;选择某一项Component列中的row行时的回调函数
 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
 - 标准的UIPickerView内容,只有字符串
 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
 
 自定义的UIPickerView内容,给每个列,行设置一个UIView对象
 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
 ----这里列为component,行row返回一个UIView用来显示在UIPickerView中。reusingView:(UIView *)

- (void)reloadAllComponents;
- (void)reloadComponent:(NSInteger)component;

DEMO实现

请添加图片描述

#import "ViewController.h"

@interface ViewController ()
//@property (nonatomic, copy)NSArray *pickerArray;
@property (nonatomic, copy)NSArray *directionArray;
@property (nonatomic, copy)NSArray *currentNameArray;
@property (nonatomic, copy)NSArray *nameArray1;
@property (nonatomic, copy)NSArray *nameArray2;
@property (nonatomic, copy)NSArray *nameArray3;
@property (nonatomic, copy)NSArray *nameArray4;
@property (nonatomic, copy)NSArray *Dict;
@property (nonatomic, copy)NSArray *endArray;
@property (nonatomic, copy)NSString *currentEndString;
@property (nonatomic, strong)UIPickerView *pickerView;
@end

@implementation ViewController

请添加图片描述

 self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 220, [UIScreen mainScreen].bounds.size.width, 200)];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
self.directionArray = @[@"EDG", @"NBA", @"Bro", @"OS"];
    self.nameArray1 = @[@"Flander", @"Jiejie", @"Scout", @"Viper", @"Mekio", @"ClearLove"];
    self.nameArray2 = @[@"KingJames", @"Curry", @"Jordan", @"Durant", @"Harden"];
    self.nameArray3 = @[@"LdQ", @"ScMokey", @"HJC"];
    self.nameArray4 = @[@"MacOS", @"iOS", @"WatchOS", @"PadOS"];
    self.endArray = @[@"LOL", @"Stars", @"Friends", @"Study"];
    self.Dict = @[_nameArray1, _nameArray2, _nameArray3, _nameArray4];

请添加图片描述

// 滑动触发事件
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    // 使用一个UIAlertView来显示用户选中列表
    if (component == 0) {
        self.currentNameArray = self.Dict[row];
        self.currentEndString = self.endArray[row];
        [self.pickerView reloadComponent:1];
        [self.pickerView reloadComponent:2];
        //这个方法的selectRow即可以表示你更新第一个列表之后是否更新第二个列表的对应的row
        // BOOL类型的YES表示动画效果
        [self.pickerView selectRow:0 inComponent:1 animated:YES];
        [self.pickerView selectRow:0 inComponent:2 animated:YES];
        
        NSLog(@"选择了%@方向", self.directionArray[row]);
    } else {
        NSLog(@"%@", self.currentNameArray[row]);
    }
}
// 返回列表的内容
#pragma mark UIPickerViewDelegate;
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
//    NSLog(@"%@", self.directionArray[row]);
    if (component == 0) {
        return self.directionArray[row];
    } else if (component == 1){
        return  self.currentNameArray[row];
    } else {
        return  self.currentEndString;
    }
}
// 返回列表的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return 130;
}

总结

原文地址:https://blog.csdn.net/weixin_61639290/article/details/127973851

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_17675.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注