有一个应用程序有一个人员列表和一个指令列表。当您在TextField中创建订单时,PickerView应显示一个员工列表(带名称)。我将按如下方式实施:
- (void)viewDidLoad {
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Employees"];
self.empl=[[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
UIPickerView *picker = [[UIPickerView alloc]init];
picker.dataSource = self;
picker.delegate = self;
[picker setShowsSelectionIndicator:YES];
[self.empTaskField setInputView:picker];
}
#pragma mark - UIPickerView DataSource Method
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return self->_empl.count;
}
#pragma mark - UIPickerView Delegate Method
-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [_empl objectAtIndex:row];
}
单击TextField上显示的错误时:
27/03/2016 23:32:57.177员工任务[2730:150090] - [Employees copyWithZone:]:无法识别的选择器发送到实例 0x7fcf2ac84fd0 2016-03-27 23:32:57.513员工任务[2730: 150090] ***因未捕获的异常终止应用程序 ' NSInvalidArgumentException',原因:' - [Employees copyWithZone:]: 无法识别的选择器发送到实例0x7fcf2ac84fd0'
如何解决这个问题?
答案 0 :(得分:0)
=>你应该作为字符串返回,所以你应该获取密钥的微粒索引行并返回它。如下面的代码
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ NSArray
Teacher *teacher = teacherForSubjectArray[row];
NSString *teacherName = [NSString stringWithFormat:@"%@ %@ %@", teacher.firstName, teacher.middleName, teacher.lastName];
return teacherName;
}
答案 1 :(得分:0)
删除mutableCopy
。 NSManagedObject不符合NSCopying协议。所以你的获取代码应该是
self.empl=[managedObjectContext executeFetchRequest:fetchRequest error:nil];