您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页UIPickerView设置默认值

UIPickerView设置默认值

来源:二三娱乐

1.遍历数组,通过文字来找到位置

// 遍历数组,获取默认值

- (void)layoutSubviews {

[super layoutSubviews];

self.location.state = _stateKey;

self.location.city = _cityKey;

self.location.district = _zoneKey;

for (NSInteger i = 0; i < provinces.count; i++) {

if ([_stateKey containsString:[provinces[i] objectForKey:@"state"]]) {

[_locatePicker selectRow:i inComponent:0 animated:YES];

[_locatePicker reloadComponent:0];

[cities removeAllObjects];

cities = [[provinces objectAtIndex:i] objectForKey:@"cities"];

[self.locatePicker selectRow:0 inComponent:1 animated:YES];

[self.locatePicker reloadComponent:1];

for (NSInteger j = 0; j < cities.count; j++) {

if ([_cityKey containsString:[cities[j] objectForKey:@"city"]]) {

[_locatePicker selectRow:j inComponent:1 animated:YES];

[_locatePicker reloadComponent:1];

[areas removeAllObjects];

areas = [[cities objectAtIndex:j] objectForKey:@"areas"];

[self.locatePicker selectRow:0 inComponent:2 animated:YES];

[self.locatePicker reloadComponent:2];

for (NSInteger k = 0; k < areas.count; k++) {

if ([_zoneKey containsString:areas[k]]) {

[self.locatePicker selectRow:k inComponent:2 animated:YES];

[_locatePicker reloadComponent:2];

}

}

}

}

}

}

}

2.初始化时设置默认值

provinces = [[NSMutableArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"area.plist" ofType:nil]];

cities = [NSMutableArray arrayWithArray:[[provinces objectAtIndex:16] objectForKey:@"cities"]];

self.location.state = [[provinces objectAtIndex:16] objectForKey:@"state"];

self.location.city = [[cities objectAtIndex:0] objectForKey:@"city"];

areas = [NSMutableArray arrayWithArray:[[cities objectAtIndex:0] objectForKey:@"areas"]];

if (areas.count > 0) {

self.location.district = [areas objectAtIndex:7];

} else{

self.location.district = @"";

}

[_locatePicker selectRow:16 inComponent:0 animated:YES];

[_locatePicker selectRow:0 inComponent:1 animated:YES];

[_locatePicker selectRow:7 inComponent:2 animated:YES];

3.UIPickerView的点击时,也要设置相对应的省市区

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

switch (component) {

case 0:

cities = [[provinces objectAtIndex:row] objectForKey:@"cities"];

[self.locatePicker selectRow:0 inComponent:1 animated:YES];

[self.locatePicker reloadComponent:1];

areas = [[cities objectAtIndex:0] objectForKey:@"areas"];

[self.locatePicker selectRow:0 inComponent:2 animated:YES];

[self.locatePicker reloadComponent:2];

_stateKey = [[provinces objectAtIndex:row] objectForKey:@"state"];

self.location.state = _stateKey;

_cityKey = [[cities objectAtIndex:0] objectForKey:@"city"];

self.location.city = _cityKey;

if ([areas count] > 0) {

self.location.district = [areas objectAtIndex:0];

} else{

self.location.district = @"";

}

break;

case 1:

areas = [[cities objectAtIndex:row] objectForKey:@"areas"];

[self.locatePicker selectRow:0 inComponent:2 animated:YES];

[self.locatePicker reloadComponent:2];

self.location.state = _stateKey;

_cityKey = [[cities objectAtIndex:row] objectForKey:@"city"];

self.location.city = _cityKey;

if ([areas count] > 0) {

self.location.district = [areas objectAtIndex:0];

} else{

self.location.district = @"";

}

break;

case 2:

if ([areas count] > 0) {

self.location.state = _stateKey;

self.location.city = _cityKey;

self.location.district = [areas objectAtIndex:row];

} else{

self.location.state = _stateKey;

self.location.city = _cityKey;

self.location.district = @"";

}

break;

default:

break;

}

}

4.设置传入的省市区的属性

@property (nonatomic, strong)NSString *stateKey;

@property (nonatomic, strong)NSString *cityKey;

@property (nonatomic, strong)NSString *zoneKey;

5.页面中创建PickerView

#pragma mark - private

- (void)createPickerView {

self.picker = [[AreaPickerView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 280, SCREEN_WIDTH, 280)];

_picker.delegate = self;

[self.view addSubview:_picker];

if (_address != nil) {

if (![_state isEqualToString:@""]) {

_picker.stateKey = _state;

_picker.cityKey = _city;

_picker.zoneKey = _district;

} else {

_picker.stateKey = _city;

_picker.cityKey = _district;

_picker.zoneKey = @"";

}

} else {

_picker.stateKey = @"辽宁省";

_picker.cityKey = @"大连市";

_picker.zoneKey = @"西岗区";

}

}

Copyright © 2019- yule263.com 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务