2016-03-27 123 views
0

有一個應用程序有一個工作人員名單和一份說明列表。並且,當您在TextField中創建訂單時,PickerView應該與員工名單(帶名稱)一起出現。如何從PickerView中的核心數據中獲取數據? iOS

- (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]; 
    } 

當你點擊文本字段出現錯誤:

27/03/2016 23 : 32 : 57.177 Employee Task [ 2730 : 150090 ] - [Employees copyWithZone:]: unrecognized selector sent to instance 0x7fcf2ac84fd0 2016-03-27 23 : 32 : 57.513 Employee Task [ 2730 : 150090 ] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [Employees copyWithZone:]: unrecognized selector sent to instance 0x7fcf2ac84fd0'

如何解決這個問題,我將實現如下呢?

+0

你的問題是這一行:'self.empl = [[managedObjectContext executeFetchRequest:fetchRequest錯誤:零] mutableCopy]''Employees'沒有實現'NSCopying'協議。 – Larme

回答

0

刪除mutableCopy。 NSManagedObject不符合NSCopying協議。所以,你的抓取代碼應該

self.empl=[managedObjectContext executeFetchRequest:fetchRequest error:nil]; 
+0

ВнастоящеевремявспискевместоnameEmployeeнаписано「NUL」在PickerView –

+0

你需要通過一個NSString作爲標題如 '返回[_empl objectAtIndex:行] valueForkey:@「empName」];' – shinoys222

0

=>您應該返回字符串作爲,所以你應該獲取的關鍵顆粒索引行並返回it..like下面的代碼

-(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; 
} 
+0

這是什麼? 「teacherForSubjectArray [row]」 –

+0

你必須通過你的情況: [_empl objectAtIndex:row]; – Akash

+0

它不解決我的問題(現在列表中PeckerView是空 –