2012-08-31 63 views
0

我想在UIPickerView禁用第1行,但它拋出我下面的錯誤禁用行0 - iOS設備

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2147483647 beyond bounds [0 .. 15]' 

任何人都可以建議我出了什麼錯在這裏?這裏是代碼

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
    AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    if ((row > 0)){ 
     NSLog(@"%@",[locationArray objectAtIndex:row]); 

     if(flag){ 

     appdelegate.selectedLocation = [locationArray objectAtIndex:row]; 
     NSLog(@"%@",appdelegate.selectedLocation); 

     } else{ 

     appdelegate.selectedLocation = [locationArray objectAtIndex:row]; 
     NSLog(@"%@",appdelegate.selectedLocation); 

    } 
     [promptLocation addSubview:tapButton]; 
     [tapButton addTarget:self action:@selector(goToDesireLoc:) forControlEvents:UIControlEventTouchUpInside]; 
    }else{ 
    //Don't do anything if row 0 is chosen. 
    NSLog(@"do nothing"); 
    } 
} 
+1

顯然你沒有正確地得到所選行的索引 – Mil0R3

回答

1

我會建議您添加異常斷點,以便您可以看到代碼的哪一行導致崩潰。看到這裏如何做到這一點:stackoverflow.com/q/4961770/472344。

這應該會導致您的代碼在導致崩潰的行中斷。您可以檢查「行」的值和「locationArray」的大小。

看起來你的行是2147483647而你的數組長度是16.所以我猜你的UIPickerView數據源可能有問題。也許發佈您的代碼爲:

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 

您還可以設置方法中的斷點,以確保它得到正確調用,並檢查它返回的值。