选择器只显示3个对象

时间:2012-04-13 08:26:42

标签: iphone ios core-data crash picker

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

收到的错误是“ *由于未捕获的异常而终止应用程序'NSRangeException',原因:'* - [_ PFArray objectAtIndex:]:index(3)超出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];

     } 

}

1 个答案:

答案 0 :(得分:0)

由于每个组件似乎依赖于其他组件的内容,看起来您应该在更改模块或主题时重新加载整个选择器,否则您可能会因为计数而错误地使用数组大小给予,这确实会给你带来的错误。

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

您的实施 -

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

......错了!

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