我有一个三组件选择器,第三个依赖于前两个。当每个组件中只有3个项目时,这种依赖关系就非常完美。当最终组件中的组件超过三个时(根据其他两个组件的值进行更改,则在尝试移动卷轴时会崩溃)。
收到的错误是“ *由于未捕获的异常'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];
}
}
最佳答案
由于每个组件的内容似乎都依赖于其他组件,因此,当您更改模块或主题时,您似乎应该重新加载整个选择器,否则冒着错误的数组大小来计算计数的风险,这确实会给您您得到的错误。
编辑-仔细查看您的代码!
您的实现-
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
...是错的!
您正在使用同一行变量一次访问所有3个数组,而不检查需要哪个数组,因此如果组件的行为16,它也将尝试为模块数组获取objectAtIndex:16。您需要以与其他方法相同的方式检查要访问的数组。
关于iphone - 选择器将仅显示3个对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10137552/