iOS实例之---国家选择器

本文介绍如何使用 UIPickerView 构建一个国家选择器,包括创建模型类 CountryInfo,利用 plist 文件加载数据,以及自定义 UIView 显示选择项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个小实例主要涉及UIPickView及其代理UIPickViewDataSource和UIPickViewDelegate

极客学院视频地址:http://www.jikexueyuan.com/course/384.html

源码地址:https://github.com/ianzhengnan/Country


实例效果:



实现步骤如下:

1 创建项目,导入图片和plist文件

2 创建模型类。

#import <Foundation/Foundation.h>

@interface CountryInfo : NSObject

@property (nonatomic, strong) NSString *name;

@property (nonatomic, strong) NSString *icon;

- (instancetype)initWithDict: (NSDictionary *)dict;

+ (instancetype)countryWithDict: (NSDictionary *)dict;


@end

//
//  CountryInfo.m
//  Country
//
//  Created by on 15/7/1.
//  Copyright (c) 2015年 zhengnan. All rights reserved.
//

#import "CountryInfo.h"

@implementation CountryInfo

- (instancetype)initWithDict: (NSDictionary *)dict{
    
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dict]; //通常实际项目中不用kvc的方式,通常都是读取的json xml的方式ß
    }
    return self;
}

+ (instancetype)countryWithDict: (NSDictionary *)dict{
    
    return [[self alloc] initWithDict:dict];
}

@end

3 懒加载导入plist文件数据

//lazy load
- (NSArray *)dataArray{

    if (!_dataArray) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"flags.plist" ofType:nil];
        NSArray *array = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *muArr = [NSMutableArray arrayWithCapacity:array.count];
        
        for (NSDictionary *dict in array) {
            CountryInfo *countryModel = [CountryInfo countryWithDict:dict];
            [muArr addObject:countryModel];
        }
        _dataArray = [muArr copy];
    }
    return _dataArray;
}


3.1 漏了一步,在main.storyboard文件中,拖入一个UIPickView.在上面点击右键通过拖动设置它的DataSource和Delegate为ViewController

并在ViewController.m文件头部加入< UIPickViewDataSource, 


4 创建用来显示国名和国旗的UIView

类文件和一个xib文件,需要他们同名


5 在xib文件中,加入一个view, 并拖入一个lable和一个UIImageView

调整他们的大小


6. view类

在其中拖入xib文件中的控件,并创建一个模型的实例。并重写模型的setter方法,把它绑定到xib文件的控件上。

//
//  CountryFlagView.h
//  Country
//
//  Created by  on 15/7/2.
//  Copyright (c) 2015年 zhengnan. All rights reserved.
//

#import <UIKit/UIKit.h>

@class CountryInfo; //为了防止循环导入,所以这里用class
@interface CountryFlagView : UIView
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UIImageView *flag;

@property (nonatomic, strong) CountryInfo *countryModel;

+ (instancetype)countryView;
+ (CGFloat)rowHeight;

@end

//
//  CountryFlagView.m
//  Country
//
//  Created by on 15/7/2.
//  Copyright (c) 2015年 zhengnan. All rights reserved.
//

#import "CountryFlagView.h"
#import "CountryInfo.h"

@implementation CountryFlagView

+ (instancetype)countryView{

    return [[[NSBundle mainBundle] loadNibNamed:@"CountryFlagView" owner:self options:nil] lastObject];
}

//assign data
- (void)setCountryModel:(CountryInfo *)countryModel{

    if (_countryModel != countryModel) {
        _countryModel = countryModel;
        self.name.text = _countryModel.name;
        self.flag.image = [UIImage imageNamed:_countryModel.icon];
        
    }
}

+ (CGFloat)rowHeight{
    return 54;
}

@end

7 实现代理UIPickViewDataSource的两个必须实现的方法

1)设置有多少个组件

2)设置每个组件有多少行

#pragma mark -UIPickViewDataSource
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.dataArray.count;
}

8 设置每行里面的View在代理UIPickViewDelegate中并绑定数据。

#pragma mark - UIPickViewDelegate
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    CountryFlagView *countryView = (CountryFlagView *)view;
    if (!countryView) {
        countryView = [CountryFlagView countryView];
    }
    
    countryView.countryModel = self.dataArray[row];
    
    return countryView;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值