自定義城市鍵盤

// 模型數(shù)據(jù)
// LZCityItem.h
#import <Foundation/Foundation.h>

@interface LZCityItem : NSObject

/** 城市*/
@property (nonatomic, strong) NSArray *cities;
/** 省名稱*/
@property (nonatomic, strong) NSString *name;

// 快速構建
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)cityItemWithDict:(NSDictionary *)dict;

@end


// LZCityItem.m
#import "LZCityItem.h"

@implementation LZCityItem

// 快速構建
- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]) {
        // KVC
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

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

@end


// LZCityTextF.h
#import <UIKit/UIKit.h>

@interface LZCityTextF : UITextField

@end

// LZCityTextF.m
#import "LZCityTextF.h"
#import "LZCityItem.h"

@interface LZCityTextF () <UIPickerViewDataSource, UIPickerViewDelegate>

/** 城市數(shù)組*/
@property (nonatomic, strong) NSArray *cityArray;
/** 選中的行,表示選中的省*/
@property (nonatomic, assign) NSInteger selectedRow;

@end

@implementation LZCityTextF

#pragma mark - 懶加載數(shù)據(jù)
- (NSArray *)cityArray
{
    if (_cityArray == nil) {
        NSString *path =  [[NSBundle mainBundle] pathForResource:@"provinces.plist" ofType:nil];
        NSArray *array =  [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *tempArray = [NSMutableArray array];
        for (NSDictionary *dict in array) {
            //把字典轉成模型
            LZCityItem *item = [LZCityItem cityItemWithDict:dict];
            [tempArray addObject:item];
        }
        _cityArray = tempArray;
    }
    return _cityArray;
}

// 代碼創(chuàng)建的時候調(diào)用
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

// 從storyboard或者xib中創(chuàng)建的時候調(diào)用
- (void)awakeFromNib
{
    [self setup];
}

// 設置
- (void)setup
{
    UIPickerView *pickV = [[UIPickerView alloc] init];
    pickV.dataSource = self;
    pickV.delegate = self;
    //修改鍵盤的類型
    self.inputView = pickV;
}

#pragma mark - UIPickerViewDataSource 方法
// 多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

// 每列多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        // 如果是第0列,表示的是省的個數(shù)
        return self.cityArray.count;
    } else {
        // 如果是第1列,表示的是市的個數(shù)
        // 思路,首先得確定第0列選中的行,通過一個成員屬性來保存
        // 1.拿到選中的省
        LZCityItem *item = self.cityArray[self.selectedRow];
        // 特定省該城市的所有個數(shù)
        return item.cities.count;
    }
}

// 每行顯示標題
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0) {
        // 選中第0列第row行
        // row表示第0列選中的行
        LZCityItem *item = self.cityArray[row];
        return item.name;
    }else {
        // 第1列
        // 思路,首先得確定第0列選中的行,通過一個成員屬性來保存
        // 選中特定的省
        LZCityItem *item = self.cityArray[self.selectedRow];
        // 選中的特定的省的特定的市
        // row表示第1列選中的行
        return item.cities[row];
    }
}

// 選中某一行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {
        // 第0列 這里面的row表示的是第0列選中的行
        self.selectedRow = row;
        // 刷新
        [pickerView reloadAllComponents];
        // 設置第1列默認滾到第0行
        [pickerView selectRow:0 inComponent:1 animated:YES];
    }

    // 通過特定的行來選中特定的省
    LZCityItem *item = self.cityArray[self.selectedRow];

    // 拿到第1列選中的行
    NSInteger selRow = [pickerView selectedRowInComponent:1];

    self.text = [NSString stringWithFormat:@"%@-%@", item.name, item.cities[selRow]];
}

@end


效果圖片:

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容