我有一个三组件选择器,第三个依赖于前两个。当每个组件中只有 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];
}
}