我覺得我的代碼是邏輯,我發現很多例子,在做這件事之前提到,但我仍然無法解決這個問題。有更多的代碼,但我只包括那些我認爲可能有關的代碼。我在actionsheet
上做了兩個部件pickerview
。 pickerview
應該根據從第一個組件中選擇的狀態顯示第二個組件的城市。它與柔佛和吉打合作很好,但是當它進入吉蘭丹和馬六甲州時,它會崩潰。我懷疑它與項目的數量有關。但我不知道如何解決它。任何建議? (讓我知道是否需要更多的代碼,將提供它)先謝謝你。UIPickerView崩潰
一些代碼組成:(i包括numberOfRowsInComponent
,titleForRow
和didSelectRow
方法和陣列)
StateList = [NSArray arrayWithObjects: @"Johor",@"Kedah",@"Kelantan",@"Wilayah Persekutuan",@"Melacca", nil];
JohorCityList = [[NSMutableArray alloc] initWithObjects: @"Kota Tinggi",@"Larkin Lama",@"Muar",@"Pasir Gudang", nil];
KedahCityList = [[NSArray alloc] initWithObjects: @"Langkawi",@"Alor Setar",@"Bakar Arang", nil];
KelantanCityList = [[NSMutableArray alloc] initWithObjects:@"Tanah Merah",@"Kota Bharu", nil];
KLCityList = [[NSArray alloc] initWithObjects: @"Batu Muda", @"Labuan",@"Putrajaya",@"Cheras", nil];
MelaccaCityList = [NSArray arrayWithObjects: @"Bandaraya Melaka", @"Bukit Rambai", nil];
(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0) {
return [StateList count];
}
else if (component == 1){
if ([selectedState isEqualToString:@"Johor"]) {
return [JohorCityList count];
}
else if ([selectedState isEqualToString:@"Kedah"]){
return [KedahCityList count];
}
else if ([selectedState isEqualToString:@"Kelantan"]) {
return [KelantanCityList count];
}
else if ([selectedState isEqualToString:@"Wilayah Persekutuan"]) {
return [KLCityList count];
}
else if ([selectedState isEqualToString:@"Melacca"]){
return [MelaccaCityList count];
}
// else
// return [JohorCityList count];
}
return [JohorCityList count];}
(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0) {
return [StateList objectAtIndex:row];
}
else if (component == 1){
if ([selectedState isEqualToString:@"Johor"]) {
return [JohorCityList objectAtIndex:row];
}
else if ([selectedState isEqualToString:@"Kedah"]){
return [KedahCityList objectAtIndex:row];
}
else if ([selectedState isEqualToString:@"Kelantan"]) {
return [KelantanCityList objectAtIndex:row];
}
else if ([selectedState isEqualToString:@"Wilayah Persekutuan"]) {
return [KLCityList objectAtIndex:row];
}
else if ([selectedState isEqualToString:@"Melacca"]) {
return [MelaccaCityList objectAtIndex:row];
}
}
return [JohorCityList objectAtIndex:row];}
(void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(@"Selected Row %d", row);
if ([thePickerView selectedRowInComponent:0] == 0){ //JB
NSLog(@"case 0");
[thePickerView reloadComponent:1];
switch (component) {
case 0:
selectedState = [StateList objectAtIndex:row];
return;
case 1:
selectedJohorCity = [JohorCityList objectAtIndex:row];
return;
} // end switch
} // end
else if ([thePickerView selectedRowInComponent:0] == 1){ //Kedah
NSLog(@"case 1");
[thePickerView reloadComponent:1];
switch (component) {
case 0:
selectedState = [StateList objectAtIndex:row];
return;
case 1:
selectedKedahCity = [KedahCityList objectAtIndex:row];
return;
} // end switch
} // end
else if ([thePickerView selectedRowInComponent:0] == 2){ //Kelantan
NSLog(@"case 2");
[thePickerView reloadComponent:1];
switch (component) {
case 0:
selectedState = [StateList objectAtIndex:row];
return;
case 1:
selectedKelantanCity = [KelantanCityList objectAtIndex:row];
return;
} // end switch
} // end
輸出:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
和SIGABRT在titleforRow方法終止於:
return [KelantanCityList objectAtIndex:row];
嗨,但選擇器視圖甚至不顯示kelantanCityList中的項目,但崩潰。我沒有訪問該列表中的任何項目。 –
@ A.K.C.F.L:我已經更新了一些您的方法和數組以獲得更好的解決方案,請檢查最新的答案。 –
哦,我的上帝!非常感謝!!!它就像一個魅力!謝謝!! –