美文网首页UI基础
国家选择器(UIPickView的基本使用)

国家选择器(UIPickView的基本使用)

作者: 952625a28d0d | 来源:发表于2016-03-16 16:07 被阅读492次
  • 创建数据模型
  • 基本控件
  • 自定义视图

数据模型

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font-variant-ligatures: no-common-ligatures; color: #000000}

#import <Foundation/Foundation.h>

@interface CountryModel : NSObject

@property(nonatomic, copy) NSString * name;
@property(nonatomic, copy) NSString * icon;

- (instancetype)initWithDictionary:(NSDictionary *)dict;
+ (instancetype)CountryWithDictionary:(NSDictionary *)dict;

@end```

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #bb2ca2}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s4 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s5 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s6 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}

import "CountryModel.h"

@implementation CountryModel

  • (instancetype)initWithDictionary:(NSDictionary *)dict{
    if (self = [super init]) {
    [self setValuesForKeysWithDictionary:dict];
    }
    return self;
    }
  • (instancetype)CountryWithDictionary:(NSDictionary *)dict{
    return [[self alloc] initWithDictionary:dict];
    }

@end```

视图

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #4f8187}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font-variant-ligatures: no-common-ligatures; color: #008400}span.s6 {font: 18.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #008400}span.s7 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s8 {font-variant-ligatures: no-common-ligatures; color: #4f8187}

#import <UIKit/UIKit.h>

@class CountryModel;    // 防止循环声明

@interface CountryFlagView : UIView

@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UIImageView *countryImage;

@property(nonatomic, strong) CountryModel * model;

+ (instancetype)makeCountryView;

+ (CGFloat)heightRow;

@end```

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #4f8187}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #008400}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s6 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s7 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s8 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s9 {font-variant-ligatures: no-common-ligatures; color: #272ad8}

import "CountryFlagView.h"

import "CountryModel.h"

@implementation CountryFlagView

  • (instancetype)makeCountryView{
    return [[[NSBundle mainBundle] loadNibNamed:@"CountryFlagView" owner:self options:nil] lastObject];
    }
  • (void)setModel:(CountryModel *)model{
    if (_model != model) {
    _model = model;
    self.name.text = _model.name;
    self.countryImage.image = [UIImage imageNamed:_model.icon];
    }
    }
  • (CGFloat)heightRow{
    return 54;
    }

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

  • (void)drawRect:(CGRect)rect {
    // Drawing code
    }
    */

@end```

调用

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #703daa}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #bb2ca2}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #008400}p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #78492a}p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #4f8187}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s6 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s7 {font: 18.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s8 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s9 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s10 {font-variant-ligatures: no-common-ligatures; color: #31595d}span.s11 {font-variant-ligatures: no-common-ligatures; color: #272ad8}

#import "ViewController.h"
#import "CountryModel.h"
#import "CountryFlagView.h"

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property(nonatomic, strong) NSArray * dataArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark - 懒加载获取Plist数据
- (NSArray *)dataArray{
    if (!_dataArray) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"flags" ofType:@"plist"];
        NSArray *array = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:array.count];
        for (NSDictionary *dict in array) {
            CountryModel *model = [[CountryModel alloc] initWithDictionary:dict];
            [mutableArray addObject:model];
        }
        _dataArray = [mutableArray copy];
    }
    return _dataArray;
}

#pragma mark - pickViewDataSourcre Delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

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

#pragma mark pickView Delegate
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    CountryFlagView *countryFlagView = (CountryFlagView *)view;
    if (!countryFlagView) {
        countryFlagView = [CountryFlagView makeCountryView];
    }
    countryFlagView.model = self.dataArray[row];
    return countryFlagView;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return [CountryFlagView heightRow];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end```

###效果

![country.gif](http:https://img.haomeiwen.com/i189984/cb477c998f30aefb.gif?imageMogr2/auto-orient/strip)

相关文章

  • 国家选择器(UIPickView的基本使用)

    创建数据模型 基本控件 自定义视图 数据模型 p.p1 {margin: 0.0px 0.0px 0.0px 0....

  • CSS 选择器

    CSS 选择器 CSS 基本选择器及其扩展 CSS 基本选择器 通配符选择器 * 元素选择器 使用标签的名称...

  • 前端学习之jQuery

    选择器 基本选择器 层级选择器(重点)、基本过滤选择器 筛选选择器 使用jQuery操作DOM 样式操作 样式属性...

  • UIPickView的使用

    一 :预期效果 :点击按钮出现 UIPickerView 背景变为半透明效果 .1 在按钮的点击事件中 //创建全...

  • CSS3学习之选择器

    一、基本选择器 基本选择器是css中使用最频繁、最基础,也是css中最早定义的选择器。 通配符选择器通配符选择器用...

  • css总结

    1,选择器 a:元素选择器 文档的元素就是最基本的选择器 b:类选择器 可以单独使用,也可以与其他元素结合使用 *...

  • iOS关于NSDate大小的比较

    前段时间做了关于喂食计划方面的APP,里面时间的设定用到了时间选择器,于是在UIPickView的基础上重...

  • CSS和JQuery选择器

    一、 基本选择器 二、组合使用 三、属性选择器 四、 CSS3属性选择器

  • jQuery选择器

    基本选择器 基本选择器是jQuery中使用最频繁的选择器,它由元素ID、Class、元素名、多个选择符组成(#id...

  • jQuery部分

    使用jQuery需要在HTML中先引用 基本选择器 id选择器: $("#id")标签选择器: $("tagNam...

网友评论

    本文标题:国家选择器(UIPickView的基本使用)

    本文链接:https://www.haomeiwen.com/subject/pziqlttx.html