美文网首页
城市选择器

城市选择器

作者: LLL_0901 | 来源:发表于2016-05-03 15:29 被阅读216次

//城市列表的plist文件百度吧

.h

//需要添加<UIPickerViewDataSource,UIPickerViewDelegate>

///城市数组@property (nonatomic, strong) NSArray *cityArray;

///选择行数@property (assign ,nonatomic) NSInteger rowInProvince;

///选择器@property (nonatomic, strong) UIPickerView *pickerView;

@property (nonatomic, assign) iddelegate;

- (id)initWithFrame:(CGRect)frame dataArray:(NSArray *)cityArray;

///显示pickerView- (void)showPickerView;

///隐藏pickerView- (void)hidePickerView;@end@protocol CityPickerViewDelegate

///点击关闭

- (void)clickCancelButton;

///点击完成

- (void)clickCompleteButtonWithString:(NSString *)contentString;


.m

- (id)initWithFrame:(CGRect)frame dataArray:(NSArray *)cityArray

{

self = [super initWithFrame:frame];

if (self)

{

self.frame = CGRectMake(0, kMainScreenHeight, kMainScreenWidth, 44+216);

self.cityArray = [[NSArray alloc] initWithArray:cityArray];

UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kMainScreenWidth, 44)];

//bgImageView.image = [UIImage imageNamed:<#(NSString *)#>];

bgImageView.backgroundColor = [UIColor colorWithRed:234.0f/255.0f green:234.0f/255.0f blue:234.0f/255.0f alpha:1];

bgImageView.userInteractionEnabled = YES;

[self addSubview:bgImageView];

///取消

UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];

cancelButton.frame = CGRectMake(0, 0, 60, 44);

[cancelButton setTitle:@"关闭" forState:UIControlStateNormal];

[cancelButton setTitleColor:[UIColor colorWithRed:237.0f/255.0f green:101.0f/255.0f blue:105.0f/255.0f alpha:1] forState:UIControlStateNormal];

[cancelButton.titleLabel setFont:[UIFont systemFontOfSize:15]];

[cancelButton addTarget:self action:@selector(clickCancelButton) forControlEvents:UIControlEventTouchUpInside];

[bgImageView addSubview:cancelButton];

///完成

UIButton *completeButton = [UIButton buttonWithType:UIButtonTypeCustom];

completeButton.frame = CGRectMake(kMainScreenWidth-60, 0, 60, 44);

[completeButton setTitle:@"完成" forState:UIControlStateNormal];

[completeButton setTitleColor:[UIColor colorWithRed:237.0f/255.0f green:101.0f/255.0f blue:105.0f/255.0f alpha:1] forState:UIControlStateNormal];

[completeButton.titleLabel setFont:[UIFont systemFontOfSize:15]];

[completeButton addTarget:self action:@selector(clickCompleteButton) forControlEvents:UIControlEventTouchUpInside];

[bgImageView addSubview:completeButton];

self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, bgImageView.frame.size.height, kMainScreenWidth, 216)];

self.pickerView.dataSource = self;

self.pickerView.delegate = self;

self.pickerView.backgroundColor = [UIColor whiteColor];

[self addSubview:self.pickerView];

}

return self;

}

#pragma mark dataSouce

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

return  2;

}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

if (component == 0)

{

return  self.cityArray.count;

}else

{

return [[[self.cityArray objectAtIndex:self.rowInProvince] objectForKey:@"Cities"] count];

}

}

#pragma mark delegate

/**

//定义字体的大小

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

{

UILabel *myView = nil;

if (component == 0) {

myView = [[UILabel alloc] init];

myView.textAlignment = NSTextAlignmentCenter;

myView.text = [[self.cityArray objectAtIndex:row] objectForKey:@"State"];

myView.font = [UIFont systemFontOfSize:15];        //用label来设置字体大小

myView.backgroundColor = [UIColor clearColor];

}else {

myView = [[UILabel alloc] init];

myView.text = [[[[self.cityArray objectAtIndex:self.rowInProvince] objectForKey:@"Cities"] objectAtIndex:row] objectForKey:@"city"];

myView.textAlignment = NSTextAlignmentCenter;

myView.font = [UIFont systemFontOfSize:15];

myView.backgroundColor = [UIColor clearColor];

}

return myView;

}

//更改组键的宽度

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component

{

CGFloat componentWidth = 0.0;

if (component == 0)

componentWidth = 100.0; // 第一个组键的宽度

else

componentWidth = 180.0; // 第2个组键的宽度

return componentWidth;

}

//更改row的高度

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component

{

return 40.0;

}

*/

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

if (component == 0) {

return [[self.cityArray objectAtIndex:row] objectForKey:@"State"];

}

else {

return [[[[self.cityArray objectAtIndex:self.rowInProvince] objectForKey:@"Cities"] objectAtIndex:row] objectForKey:@"city"];

}

}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

if (component == 0){

self.rowInProvince = row;

[self.pickerView reloadComponent:1];

}

}

///显示pickerView

- (void)showPickerView

{

[UIView animateWithDuration:0.3 animations:^{

self.frame = CGRectMake(0, kMainScreenHeight-44-216, kMainScreenWidth, self.frame.size.height);

} completion:^(BOOL finished) {

;

}];

}

///隐藏pickerView

- (void)hidePickerView

{

[UIView animateWithDuration:0.3 animations:^{

self.frame = CGRectMake(0, kMainScreenHeight, kMainScreenWidth, self.frame.size.height);

} completion:^(BOOL finished) {

;

}];

}

///关闭

- (void)clickCancelButton

{

[self hidePickerView];

if ([_delegate respondsToSelector:@selector(clickCompleteButtonWithString:)])

{

[_delegate clickCancelButton];

}

}

///完成

- (void)clickCompleteButton

{

[self hidePickerView];

NSString *titleString = [NSString stringWithFormat:@"%@ %@",[[self.cityArray objectAtIndex:[self.pickerView selectedRowInComponent:0]] objectForKey:@"State"],[[[[self.cityArray objectAtIndex:self.rowInProvince] objectForKey:@"Cities"] objectAtIndex:[self.pickerView selectedRowInComponent:1]] objectForKey:@"city"]];

if ([_delegate respondsToSelector:@selector(clickCompleteButtonWithString:)])

{

[_delegate clickCompleteButtonWithString:titleString];

}

}

相关文章

  • IOS弹窗选择器(实时更新)

    城市选择器-城市数据为json文件 城市选择Demo 年月日选择器 年月日Demo 相册头像剪切 相册头像剪切De...

  • 视频讲解 vue city selector 城市选择器

    vue city selector B站地址 城市选择器,以选择器的形式将中国各个省市级城市弹出,供用户选择,视频...

  • iOS开发之UITableView联动实现城市选择器

    在 iOS开发之城市选择器一文中用两列的UIPickerView实现了城市选择器,今天用两个UITableView...

  • 城市选择器

    //城市列表的plist文件百度吧 .h //需要添加 ///城市数组@property (nonatomic, ...

  • 城市选择器

    网上的城市选择器很多,但还是亲自动手实现一下,效果如下图所示 思路:使用RecyclerView的吸附式ItemD...

  • Swift.地址选择器

    实现效果: controller弹出时:半透明背景渐变展示.地址选择器从下方弹出. 地址选择器:以省份,城市,地区...

  • JS插件

    移动端城市选择器 mobileSelect.js --------- https://github.com/on...

  • vue城市选择器

    参考网址:https://distpicker.uine.org1,安装依赖npm install v-distp...

  • iOS城市选择器

    开发准备 获取城市的信息,我这里是从网上搜的一个plist文件,如果信息不全大家直接可以在里面补充自己所需的信息....

  • iOS城市选择器

网友评论

      本文标题:城市选择器

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