2014-10-20 2 views
0

이 내 데이터 모델코어 데이터 : NSFetchedResultsController

내의 TableView에서

enter image description here

, 나는 내가 그들을 가져 오는 경우

다음 firstcity 내 코드 표시 할. 내가

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"TableCell"; 
    TableCell *cell = (TableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    if (cell == nil) 
    { 
     cell = [[TableCell alloc] initWithStyle:UITableViewCellStyleDefault 
           reuseIdentifier:CellIdentifier]; 
    } 

    [self configureCell:cell atIndexPath:indexPath]; 

    return cell; 
} 

-(void)configureCell:(TableCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    NSManagedObject *obj = [fetchedResultsController objectAtIndexPath:indexPath]; 

// cell.lblName.text = @""; 
    [cell.lblName setText:[obj valueForKey:@"first"]]; 
// cell.lblCity.text = @""; 
    [cell.lblCity setText:[obj valueForKey:@"city"]]; 
} 

답변

0

가 귀하 가져온 결과 컨트롤러가 구성되어 가져온 결과를 표시하려고 어디 내가 결과를 가져 오는 곳입니다

-(NSFetchedResultsController *)fetchedResultsController 
{ 
    if(fetchedResultsController != nil) 
    { 
     return fetchedResultsController; 
    } 

    NSManagedObjectContext *context = [self managedObjectContext]; 

    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entityPerson = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context]; 
    [request setEntity:entityPerson]; 

    NSSortDescriptor *sortName = [[NSSortDescriptor alloc] initWithKey:@"first" ascending:YES]; 
    NSSortDescriptor *sortAddress = [[NSSortDescriptor alloc] initWithKey:@"address.city" ascending:NO]; 
    request.sortDescriptors = @[sortAddress, sortName]; 
    [request setFetchBatchSize:50]; 

    NSFetchedResultsController *fetchedResults = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:nil]; 
    fetchedResultsController = fetchedResults; 
    fetchedResultsController.delegate = self; 

    return fetchedResultsController; 
} 

및 다음 나는 first 데이터가 아닌 city 받고 있어요 Person 객체를 반환하려면 간접적으로 관계의 속성을 간접적으로 참조해야합니다.

[cell.lblCity setText:[obj valueForKeyPath:@"address.city"]]; 

또는 subc를 만든 경우

Person *obj = [fetchedResultsController objectAtIndexPath:indexPath]; 
cell.lblName.text = obj.first; 
cell.lblCity.text = obj.address.city; 
+0

@VNJ 미안 해요, 난 사용했다'valueForKey :'내가해야'valueForKeyPath : NSManagedObject의 아가씨, 당신은 당신의 코드를 수정할 수 있습니다, Person이라고한다'- 내가 수정하는 내 대답을 개정했다. – pbasdf