2014-01-07 74 views
1

我覺得我的代碼是邏輯,我發現很多例子,在做這件事之前提到,但我仍然無法解決這個問題。有更多的代碼,但我只包括那些我認爲可能有關的代碼。我在actionsheet上做了兩個部件pickerviewpickerview應該根據從第一個組件中選擇的狀態顯示第二個組件的城市。它與柔佛和吉打合作很好,但是當它進入吉蘭丹和馬六甲州時,它會崩潰。我懷疑它與項目的數量有關。但我不知道如何解決它。任何建議? (讓我知道是否需要更多的代碼,將提供它)先謝謝你。UIPickerView崩潰

一些代碼組成:(i包括numberOfRowsInComponenttitleForRowdidSelectRow方法和陣列)

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]; 

回答

4

它是因爲你有2個項目在KelantanCityList數組,並且您正在嘗試訪問數組的第三個對象。

更新

更具動態,你可以用兩個陣列一個與StateNames和第二位城市的數組的數組,可以顯示下面的實現:

viewDidLoad中

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
StateList = [NSMutableArray array]; 
StateNames= [[NSMutableArray alloc]initWithObjects:@"Johor",@"Kedah",@"Kelantan",@"Wilayah Persekutuan",@"Melacca", nil]; 

JohorCityList = [[NSMutableArray alloc] initWithObjects: @"Kota Tinggi",@"Larkin Lama",@"Muar",@"Pasir Gudang", nil]; 

KedahCityList = [[NSMutableArray alloc] initWithObjects: @"Langkawi",@"Alor Setar",@"Bakar Arang", nil]; 

KelantanCityList = [[NSMutableArray alloc] initWithObjects:@"Tanah Merah",@"Kota Bharu", nil]; 

KLCityList = [[NSMutableArray alloc] initWithObjects: @"Batu Muda", @"Labuan",@"Putrajaya",@"Cheras", nil]; 

MelaccaCityList = [[NSMutableArray alloc ]initWithObjects: @"Bandaraya Melaka", @"Bukit Rambai", nil]; 

[email protected]"Johor"; 



[PickerView reloadAllComponents]; 



[StateList addObject:JohorCityList]; 
[StateList addObject:KedahCityList]; 
[StateList addObject:KelantanCityList]; 
[StateList addObject:KLCityList]; 
[StateList addObject:MelaccaCityList]; 

} 

Picker DelegateMethod

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    return 2; 
} 

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    if (component == 0) { 
     return [StateList count]; 
    } 
    { 

     int r= [[StateList objectAtIndex:[pickerView selectedRowInComponent:0]]count]; 
     return r; 
    } 


} 

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    if (component == 0) { 
     return [StateNames objectAtIndex:row]; 
    } 
    else 
    { 
    return [[StateList objectAtIndex:[pickerView selectedRowInComponent:0]]objectAtIndex:row]; 

    } 
    return @""; 
} 

-(void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 
    if (component==0) 
    { 
     [thePickerView reloadComponent:1]; 

    } 
    else 
    { 
     NSLog(@"selected State:%@ ", [StateNames objectAtIndex:[thePickerView selectedRowInComponent:0]]); 
     NSLog(@"selcted city:%@",[[StateList objectAtIndex:[thePickerView selectedRowInComponent:0]]objectAtIndex:row]); 
    } 

} 
+0

嗨,但選擇器視圖甚至不顯示kelantanCityList中的項目,但崩潰。我沒有訪問該列表中的任何項目。 –

+0

@ A.K.C.F.L:我已經更新了一些您的方法和數組以獲得更好的解決方案,請檢查最新的答案。 –

+0

哦,我的上帝!非常感謝!!!它就像一個魅力!謝謝!! –

1

這基本上意味着你試圖讓對象超出KelantanCityList的範圍。 您正嘗試在索引2處獲取元素,而數組只有2個元素(數組中的最後一個索引爲1)。

您應該調試它並查看發生了什麼,它可能是一個錯誤值selectedState這會導致您試圖從錯誤的數組中檢索數據。爲確保檢查你將嘗試從數組引用的索引是否沒有超出範圍,將有助於避免崩潰,但它不會解決可能爲selectedState設置不正確值的基本問題。

+0

嗨,我試着檢查數組。輸出顯示在selectedState中選擇的正確項目,自動(在所有輸出旁邊)也顯示正確的項目。 –

+0

有沒有可能修改數組的內容? –

+0

感謝您的幫助。讓它與上面的答案一起工作.. –