私は3つのコンポーネントのピッカーを持っており、3番目は最初の2つに依存しています。この依存関係は、各コンポーネントに3つのアイテムしかない場合に最適に機能します。最終コンポーネントに3つ以上ある場合(他の2つの値に基づいて変化します)、リールを移動しようとするとクラッシュします。
受け取ったエラーは「*キャッチされなかった例外'NSRangeException'が原因でアプリを終了しています。理由:'* -[_ PFArray objectAtIndex:]:インデックス(3)が境界を超えています(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];
}
}