2010-11-20 2 views
0

그래서 코어 데이터 객체가 있는데, 세션이라고 부르 자고 (실제로는 실제로 호출 된 것입니다.) 네 개의 속성 (이름, 드라이버, 트랙 및 자동차)이 있습니다. 테이블보기. 전에는 작동하도록 했었지만, 아쉽게도 뷰 컨트롤러를 좀 더 일반적이고 재사용 가능하도록 만들려고 노력 중입니다. 그래서 조금 변경하고 있습니다. 됐건, 여기에 뷰 컨트롤러에 전달 alt textUITableView 및 CoreData - 특정 행 및 효율성의 데이터 표시?

는 세션, CoreData 나를 위해 채찍 것을 NSManagedObject의 서브 클래스입니다 ... 테이블의 모습입니다. Driver, Car 및 Track은 모두 객체 관계이며 name은 단순히 문자열입니다. 운전사, 차 및 대위에는 모두이 테이블에서 표시하고있는 이름 속성이 있습니다. 나는이 텍스트를 테이블에 표시하는 빠르고 더러운 방법을 원했습니다. 그래서, 내가 인스턴스 변수로 세션을 통과 시작하기 전에이 대신 특정 문자열, 드라이버, 자동차와 트랙 객체를 추적하는 중, 일

NSDictionary *parameterValues = [[NSDictionary alloc] initWithObjectsAndKeys: sessionName, [NSNumber numberWithInt: 0], sessionDriver, [NSNumber numberWithInt: 1], sessionCar, [NSNumber numberWithInt: 2], sessionTrack, [NSNumber numberWithInt: 3], nil]; 

NSString *parameterString; 
if([indexPath row] > 0) { 
    if([parameterValues objectForKey: [NSNumber numberWithInt: [indexPath row]]] == [NSNull null]) { 
     parameterString = [[NSString alloc] initWithFormat: @"Select a %@", [parameterNames objectAtIndex: [indexPath row]]]; 
    } else{ 
     parameterString = [[parameterValues objectForKey: [NSNumber numberWithInt: [indexPath row]]] name]; 
    } 
} else{ 
    parameterString = [parameterValues objectForKey: [NSNumber numberWithInt: 0]]; 
    if([parameterString isEqualToString: @""]) { 
     parameterString = @"Enter A Name"; 
    } 
} 

... 같은 일을하고 있었다. [[self session] driver]는 새 세션 객체가 전달 될 때 nil을 반환하므로 사전 객체는 사용할 수 없습니다. 이것이 내가 지금하는 방법입니다 ...

//these come in handy, they're the object names (We can use KVC), and we can use them in the table titles 
NSArray *parameterNames = [[NSArray alloc] initWithObjects: @"Name", @"Driver", @"Car", @"Track", nil]; 

//get the object for this row... (Name, Driver, Car, Track), and create a string to hold it's value.. 
id object = [session valueForKey: [parameterNames objectAtIndex: [indexPath row]]]; 
NSString *parameterValue; 

NSLog(@"%@", [session name]); 

//if this isn't the name row... 
if(object != nil) { 
    //if the indexPath is greater than 0, object is not name (NSString) 
    if([indexPath row] > 0) { 
     parameterValue = [object name]; 
    } else{ 
     parameterValue = object; 
    } 
} else{ 
    //object doesn't exist yet... placeholder! 
    parameterValue = [@"Select a " stringByAppendingString: (NSString *)[parameterNames objectAtIndex: [indexPath row]]]; 
} 

내가 뭐라 구요 ... 제가이 일을 제대로하고 있습니까?

감사합니다, 매트 - 코어 데이터 초보자 :/

답변

0

당신이 생각하는 이상이다. 당신은 세션이 같은 개체가있는 경우 :

Session{ 
    name:string 
    driver<-->Driver 
    car<-->Car 
    track<-->Track 
} 

및 드라이버, 자동차를 모두 추적 name 속성이, 당신이 할 일은 그렇게 같은 속성 값을 요청 고정 된 테이블입니다 웁니다 :

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell 
             forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    cell=//... get cell however you do it 
    switch (indexPath.row) { 
     case 0: 
      cell.textLabel.text=self.currentSession.name 
      break; 
     case 1: 
      cell.textLabel.text=self.currentSession.driver.name; 
      break; 
     //... and so on 
     default: 
      break; 
    } 
    //... return the cell. 
} 

마찬가지로 객체를 상세보기로 전달하려면 동일한 switch 문을 사용하여 선택한 행과 연관된 객체를 가져옵니다.

+0

나는 이것을하고 있었지만, 어떤 이유로 나는 더 우아한 해결책을 찾고 있었다. 이제 나는 그것을 보았습니다, 이것은 아마도 더 효율적인 방법 일 것입니다 ... - 유일한 것은 switch 문 각각 안에 객체가 nil인지 검사해야한다는 것입니다. , 어수선하게. 음, 다시 한번 보도록하겠습니다. - 감사합니다 : D –

+0

nil 관계에 대한 걱정이 가장 쉬운 해결책은 관계를 필요로하고 UI에 표시 할 엔티티 기본값을 제공하는 것입니다. 예 : 세션에는 필수적인 드라이버 관계가 있으며 드라이버 엔티티의 기본 이름은 "없음"입니다. 그렇게하면 아무런 점검없이 원하는 동작과 UI를 얻을 수 있습니다. – TechZen

+0

그게 내가 생각한거야, 내가 내가 미친 줄 알았지 만. - 고마워 –