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 = @"西岗区";

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • CIDetector 这个api是苹果在ios8之后提供的。所以用苹果自带的AVFundation扫描,如果从相册...
    然亦伞阅读 576评论 1 0
  • 不为明日思量,明日自有安排。 “当下,缺什么?”这是一个不需要大脑就能回答的强有力的问题,目的是让你将注意力集中在...
    vivien90阅读 374评论 0 0
  • 山东省淄博市博山区域城镇中心小学五(1)班的孩子们开始了《小艾多的世界》阅读。家长都比较忙,我和孩子们在学校共读。...
    博山区四十亩地小学李铭阅读 610评论 0 1
  • 我21岁,对自己充满了失望。超级喜欢熬夜,自制力极差,每次决定早睡都不会成功,做事情三天打鱼两天晒网。穷逼死宅还追...
    畅菇凉CC的小木屋阅读 206评论 0 0