You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

144 lines
4.6 KiB

//
// citysCell.m
// b
//
// Created by kaidan on 2017/1/12.
// Copyright © 2017年 kaidan. All rights reserved.
//
#import "Masonry.h"
#import "citysCell.h"
#import "CityModelData.h"
#import "MySingleton.h" //单例类
#import <MJExtension.h> // 数据转模型类
#import "CusDatePickerWithArea.h" // pickView
#define changeDesc @"changeDesc"
#define selectCityIndex @"selectCityIndex" // 选中城市的下标
#define screenWidthW [[UIScreen mainScreen] bounds].size.width
#define screenHeightH [[UIScreen mainScreen] bounds].size.height
@interface citysCell (){
CusDatePickerWithArea *pickArea; // PickView视图
}
/**
* 城市模型数据
*/
@property (nonatomic,strong)CityModelData *cityModel;
@property(nonatomic,strong)UIButton* clickBtn;
@property(nonatomic,strong)UILabel* titleLab;
@end
@implementation citysCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.textLabel.text = @"选择城市";
[self.clickBtn setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[self.clickBtn addTarget:self action:@selector(showCitys) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.clickBtn];
[self.clickBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.height.equalTo(self);
make.width.mas_equalTo(self).multipliedBy(0.5);
make.left.mas_equalTo(self.mas_centerX);
}];
self.titleLab = [[UILabel alloc] init];
self.titleLab.text = @"广东省 广州市";
self.titleLab.textAlignment = NSTextAlignmentLeft;
[self.clickBtn addSubview:self.titleLab];
[self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.left.top.equalTo(self.clickBtn);
make.right.mas_equalTo(self.clickBtn).offset(-30);
}];
}
return self;
}
-(UIButton *)clickBtn{
if (!_clickBtn) {
_clickBtn = [[UIButton alloc] init];
}
return _clickBtn;
}
-(CityModelData *)cityModel{
if (_cityModel==nil) {
MySingleton *mySing=[MySingleton shareMySingleton];
if (mySing.cityModel) {
_cityModel=mySing.cityModel;
}
else{
NSString *jsonPath=[[NSBundle mainBundle]pathForResource:@"province_data.json" ofType:nil];
NSData *jsonData=[[NSData alloc]initWithContentsOfFile:jsonPath];
NSString *stringValue=[[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
NSDictionary *dicValue=[mySing getObjectFromJsonString:stringValue]; // 将本地JSON数据转为对象
_cityModel=[CityModelData mj_objectWithKeyValues:dicValue];
mySing.cityModel=_cityModel;
}
}
return _cityModel;
}
#pragma mark 上次选中的值
-(void)lastSelectValue{
NSString *strLastSelectValue=[MySingleton getsaveLoacalField:changeDesc]; // 得到本地存储的上次选中的值
if(strLastSelectValue.length>0){
[self.clickBtn setTitle:strLastSelectValue forState:UIControlStateNormal];
}
}
#pragma mark 添加pickView
-(void)addPickView{
__weak typeof(self) lowSelf=self;
pickArea=[[CusDatePickerWithArea alloc]initWithFrame:CGRectMake(0, 0, screenWidthW, screenHeightH) cityData:lowSelf.cityModel];
NSString *stringSelectCityIndex=[MySingleton getsaveLoacalField:selectCityIndex]; // 得到上次保存的下标
if (stringSelectCityIndex.length>0) {
NSArray *arrIndex=[stringSelectCityIndex componentsSeparatedByString:@"-"];
if (arrIndex.count==3) {
[pickArea scrollToRow:[arrIndex[0] integerValue] secondRand:[arrIndex[1] integerValue] thirdRand:[arrIndex[2] integerValue]]; // 滚动到选择的位置
}
}
else{
[pickArea scrollToRow:23 secondRand:0 thirdRand:4]; // 没有选择的情况下,默认滚动到上海
}
UIWindow *myWindow=[[UIApplication sharedApplication]keyWindow];
[myWindow addSubview:pickArea];
pickArea.areaValue=^(NSString *value,NSString *indexString){ //Block回调传值
lowSelf.titleLab.text = value;
[MySingleton saveLoacalWithField:changeDesc value:value]; // 保存对应的数据和选中的下标
[MySingleton saveLoacalWithField:selectCityIndex value:indexString];
};
}
-(void)showCitys{
[self addPickView]; // 添加选中视图
}
-(NSString *)cityStr{
return self.titleLab.text;
}
@end