我觉得我的代码是逻辑的,我在做这个之前发现了许多例子,但我仍然无法解决这个问题。有更多的代码,但我只包括我认为可能相关的代码。我在pickerview
上做了两个组件actionsheet
。 pickerview
应该根据从第一个组件中选择的状态显示第二个组件中的城市。这对柔佛和吉打很有效,但是当它进入吉兰丹州和马六甲州时,它会崩溃。我怀疑它与物品的数量有关。但我不知道如何解决它。有什么建议吗?
(如果需要更多代码,请告知我们),请提前感谢您。
部分代码:(我包括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];
答案 0 :(得分:2)
崩溃因为您在 KelantanCityList 数组中有2个项目,并且您正在尝试访问数组的第3个对象。
<强>更新强>
对于更多动态,您可以使用两个带有StateNames的数组,第二个带有City of Array的数组,您可以显示以下实现:
的 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];
selectedState=@"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]);
}
}
答案 1 :(得分:1)
这基本上意味着你试图让对象超出KelantanCityList
的范围。 您正尝试在索引2处获取元素,而数组只有2个元素(数组中的最后一个索引为1)。
你应该调试它并查看发生了什么,它可能是selectedState
的一个错误值,导致你试图从错误的数组中检索数据。确保检查您将尝试从数组引用的索引是否不受约束将帮助您避免崩溃,但它无法解决可能为selectedState
设置不正确值的基本问题。