UIPickerView設(shè)置默認(rèn)值

1.遍歷數(shù)組,通過文字來找到位置

// 遍歷數(shù)組,獲取默認(rèn)值

- (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.初始化時(shí)設(shè)置默認(rèn)值

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的點(diǎn)擊時(shí),也要設(shè)置相對(duì)應(yīng)的省市區(qū)

- (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.設(shè)置傳入的省市區(qū)的屬性

@property (nonatomic, strong)NSString *stateKey;

@property (nonatomic, strong)NSString *cityKey;

@property (nonatomic, strong)NSString *zoneKey;

5.頁面中創(chuàng)建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 = @"西崗區(qū)";

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市录粱,隨后出現(xiàn)的幾起案子卑惜,更是在濱河造成了極大的恐慌务豺,老刑警劉巖棒旗,帶你破解...
    沈念sama閱讀 222,183評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件痊乾,死亡現(xiàn)場(chǎng)離奇詭異皮壁,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)哪审,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門蛾魄,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事滴须∩喙罚” “怎么了?”我有些...
    開封第一講書人閱讀 168,766評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵描馅,是天一觀的道長把夸。 經(jīng)常有香客問我,道長铭污,這世上最難降的妖魔是什么恋日? 我笑而不...
    開封第一講書人閱讀 59,854評(píng)論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮嘹狞,結(jié)果婚禮上岂膳,老公的妹妹穿的比我還像新娘。我一直安慰自己磅网,他們只是感情好谈截,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著涧偷,像睡著了一般簸喂。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上燎潮,一...
    開封第一講書人閱讀 52,457評(píng)論 1 311
  • 那天喻鳄,我揣著相機(jī)與錄音,去河邊找鬼确封。 笑死除呵,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的爪喘。 我是一名探鬼主播颜曾,決...
    沈念sama閱讀 40,999評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼秉剑!你這毒婦竟也來了泛豪?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,914評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤秃症,失蹤者是張志新(化名)和其女友劉穎候址,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體种柑,經(jīng)...
    沈念sama閱讀 46,465評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡岗仑,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評(píng)論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了聚请。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片荠雕。...
    茶點(diǎn)故事閱讀 40,675評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡稳其,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出炸卑,到底是詐尸還是另有隱情既鞠,我是刑警寧澤,帶...
    沈念sama閱讀 36,354評(píng)論 5 351
  • 正文 年R本政府宣布盖文,位于F島的核電站嘱蛋,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏五续。R本人自食惡果不足惜洒敏,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評(píng)論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望疙驾。 院中可真熱鬧凶伙,春花似錦、人聲如沸它碎。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽扳肛。三九已至傻挂,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間挖息,已是汗流浹背踊谋。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評(píng)論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留旋讹,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,091評(píng)論 3 378
  • 正文 我出身青樓轿衔,卻偏偏與公主長得像沉迹,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子害驹,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評(píng)論 2 360

推薦閱讀更多精彩內(nèi)容