0

我有一个三组件选择器,第三个依赖于前两个。当每个组件中只有 3 个项目时,这种依赖关系非常有效。当最终组件中超过三个时(根据其他两个的值而变化,它会在尝试移动卷轴时崩溃。

收到的错误是“ * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[_PFArray objectAtIndex:]: index (3) beyond bounds (3)'”

我无法弄清楚它从哪里得到这个 3 的界限。这不是完整的实现,我在视图中确实加载了填充使用的数组和一个带有逻辑的按钮来使用按钮按下时的值。如果您认为这些可能是问题,请告诉我。我感谢任何意见或帮助。谢谢

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

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

    NSString *message = [[NSString alloc] initWithFormat:@"%d", moduleCount];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Module selected" message:message delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];

    [alert show];

    if (component == kPartComponent) {
        return [self.parts count];
    }
    else if (component == kSubjectComponent) {
        return [self.courses count];
    }
    else return moduleCount; // problem is likely to be here where i return the count, the picker is only showing 3 modules for each section not the full amount





}

#pragma mark Picker Delegate Methods
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {



    Parts *p = [self.parts objectAtIndex:row];

    Course *c = [self.courses objectAtIndex:row];

    Modules *m = [self.modules objectAtIndex:row];


    if (component == kPartComponent) {
        return p.part;
    }

    else if (component == kModuleComponent) {
        return m.name;
    }

    else return c.course;


}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if(component == 0)
        return 40.0;
    else if(component == 1)
        return 85.0;
    else
        return 125.0;

}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {


     if (component == kSubjectComponent) { 

     Course *selectedCourse;
     selectedCourse = [self.courses objectAtIndex:row];
     NSString *courseComp = [selectedCourse valueForKey:@"course"];

     AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

     NSManagedObjectContext *context = [delegate managedObjectContext];

     NSEntityDescription *entityModules = [NSEntityDescription entityForName:@"Modules" inManagedObjectContext:context];

     NSFetchRequest *fetchRequestModules = [[NSFetchRequest alloc] init];
     [fetchRequestModules setEntity:entityModules];

     NSPredicate * modSubCondition = [NSPredicate predicateWithFormat:@"(courses.course == %@)", courseComp];
     NSPredicate * modPartCondition = [NSPredicate predicateWithFormat:@"(parts.part == %@)", globalPart];
     NSPredicate * compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:modSubCondition, modPartCondition, nil]];

     [fetchRequestModules setPredicate:compoundPredicate]; 

     NSSortDescriptor *sortDescriptorModules = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; // This definitely works, proven by changing the ascending bool.
     NSArray *sortDescriptorsModules = [[NSArray alloc] initWithObjects:sortDescriptorModules, nil]; 

     [fetchRequestModules setSortDescriptors:sortDescriptorsModules];

     NSArray *modulesArray = [context executeFetchRequest:fetchRequestModules error:nil];


         globalSubject = courseComp;

     self.modules = modulesArray;

         moduleCount = [self.modules count];



     [picker selectRow:0 inComponent:kModuleComponent animated:YES];
     [picker reloadComponent:kModuleComponent];
     } 

     else if (component == kPartComponent) { 

     Parts *selectedParts;
     selectedParts = [self.parts objectAtIndex:row];
     NSString *partsComponent = [selectedParts valueForKey:@"part"];

     AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

     NSManagedObjectContext *context = [delegate managedObjectContext];

     NSEntityDescription *entityModules = [NSEntityDescription entityForName:@"Modules" inManagedObjectContext:context];

     NSFetchRequest *fetchRequestModules = [[NSFetchRequest alloc] init];
     [fetchRequestModules setEntity:entityModules];

     NSPredicate * modSubCondition = [NSPredicate predicateWithFormat:@"(courses.course == %@)", globalSubject];
     NSPredicate * modPartCondition = [NSPredicate predicateWithFormat:@"(parts.part == %@)", partsComponent];
     NSPredicate * compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:modSubCondition, modPartCondition, nil]];

     [fetchRequestModules setPredicate:compoundPredicate]; 

     NSSortDescriptor *sortDescriptorModules = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; // This definitely works, proven by changing the ascending bool.
     NSArray *sortDescriptorsModules = [[NSArray alloc] initWithObjects:sortDescriptorModules, nil]; 

     [fetchRequestModules setSortDescriptors:sortDescriptorsModules];

     NSArray *modulesArray = [context executeFetchRequest:fetchRequestModules error:nil];


         globalPart = partsComponent;

     self.modules = modulesArray;

         moduleCount = [self.modules count];



     [picker selectRow:0 inComponent:kModuleComponent animated:YES];
     [picker reloadComponent:kModuleComponent];

     } 

}
4

1 回答 1

0

由于每个组件的内容似乎都依赖于其他组件,因此当您更改模块或主题时,您似乎应该重新加载整个选择器,否则您可能会冒着为您提供的计数设置错误大小的数组的风险,这确实会给你你得到的错误。

编辑 - 仔细查看您的代码!

您的实施 -

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

...是错的!

您正在使用相同的行变量一次访问所有 3 个数组,而不检查需要哪一个,所以如果行是 16,对于组件,它也将尝试为您的模块数组获取 objectAtIndex:16。您需要检查您正在访问的数组,就像您在其他方法中所做的那样。

于 2012-04-13T08:59:08.783 回答