2010-05-28 2 views
1

술어를 사용하여 코어 데이터에서 오브젝트를 찾습니다. 원하는 객체를 성공적으로 찾을 수 있지만 그 객체의 indexPath를 가져와 해당 객체에 대한 상세 뷰를 푸시 할 수 있어야합니다. 현재 객체를 가져 오는 데 필요한 다음 코드가 있습니다.iPhone : 술어 오브젝트의 인덱스 경로 얻기

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"Ride" inManagedObjectContext:self.managedObjectContext]]; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title = %@ AND addressFull = %@", view.annotation.title, view.annotation.subtitle]; 
    [fetchRequest setPredicate:predicate]; 
    NSMutableArray *sortDescriptors = [NSMutableArray array]; 
    [sortDescriptors addObject:[[[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES] autorelease]]; 
    [sortDescriptors addObject:[[[NSSortDescriptor alloc] initWithKey:@"addressFull" ascending:YES] autorelease]]; 
    [fetchRequest setSortDescriptors:sortDescriptors]; 
    [fetchRequest setReturnsObjectsAsFaults:NO]; 
    [fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"title", @"addressFull", nil]]; 
    NSError *error = nil; 
    NSArray *fetchedItems = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error]; 
    // Sohow what record we returned 
    NSLog(@"%@",[fetchedItems objectAtIndex:0]); 

따라서 개체를 배열로 올바르게 가져올 수 있습니다. 그러나이 객체를 indexPath로 변환하려면 어떻게해야합니까?

답변

2

인덱스 경로는 인덱스 집합입니다. {0, 2}은 테이블 뷰의 첫 번째 섹션과 세 번째 행을 가리키는 인덱스 경로를 나타낼 수 있습니다. 배열 데이터의 테이블 뷰 표현이 궁극적 인 목표라고 가정합니다.

인덱스 경로는 배열의 인덱스로 경로를 변환하는 방법에 따라 배열의 특정 개체를 가리킬 수 있습니다. 당신이 임의의 인덱스 경로를 확인하려면

, 그것은 꽤 쉽게 : 그래서 당신은 당신의 배열 구조가 다시 섹션과 행 (로 변환하는 방법을 파악한다 무엇을해야하는지

NSUInteger myFirstIndex = 0; 
NSUInteger mySecondIndex = 2; 
NSUInteger myIndices[] = {myFirstIndex, mySecondIndex}; 
NSIndexPath *myIndexPath = [[NSIndexPath alloc] initWithIndexes:myIndices length:2]; 
// ...do someting with myIndexPath... 
[myIndexPath release]; 

, 당신이 원하는 가정 테이블 뷰 표현을 만든다).

또 다른 옵션은 NSFetchedResultsController을 사용하여 테이블 뷰 업데이트를 처리하는 것입니다.이 섹션은 섹션을 분할하는 방법에 따라 색인 경로를 처리합니다.

+0

예, 맞습니다. UITableView에 표시하고 싶습니다. 클릭 한지도 주석에 대한 세부 정보보기에서 슬라이드하기 만하면됩니다. –

+0

데이터 가져 오기를'NSFetchedResultsController'에 넣고 테이블 뷰 업데이트를하도록 권합니다. 코어 데이터 가져 오기 결과를 테이블 뷰에 연결하는 작업의 약 99 %가 제거됩니다. –

+0

나는 루트 컨트롤러로 init 할 필요가있다. –