2012-07-14 4 views
1

안녕하세요. 핵심 데이터에 데이터를 저장하려고하는데 문제가 있습니다. 팀 엔티티와 플레이어 엔티티가 있습니다. 팀 엔티티는 다음과 같이 일대 다 관계로 설정되어 있습니다. Player Entity ... 내 "NewTeamViewController"에는 두 개의 섹션이 있고, 두 번째 섹션에는 플레이어를 팀에 추가하는 곳이 있습니다. 섹션 헤더에는 새로운 플레이어를 추가하는 버튼이 있습니다 ... 버튼을 누르면 세 개의 textField가있는 새로운 셀이 나타나며 각 셀에는 기본 텍스트 (자리 표시 자 텍스트가 아님)가 있고 그 다음 새 플레이어를 MutableSet에 추가합니다 그것은 팀 선수로 추가됩니다. tableview는 세 개의 textField가있는 사용자 정의 셀을 사용합니다.코어 데이터에있는 테이블 뷰 셀의 데이터 저장

플레이어 셀의 세 텍스트 필드에서 데이터를 저장할 수 없다는 점을 제외하면 팀이 올바르게 저장됩니다. 그냥 플레이어의 셀에 기본 텍스트를 저장합니다.

새로 추가 한 셀의 데이터를 새로 추가 한 플레이어 개체에 제공하는 방법이나 위치를 잘 모르겠습니다. 여기

-(void)saveButtonWasPressed { 

self.team =[NSEntityDescription insertNewObjectForEntityForName:@"Team" inManagedObjectContext:self.managedObjectContext]; 

team.schoolName = _schoolName.text; 
team.teamName = _teamName.text; 
team.season = _season.text; 
team.headCoach = _headCoach.text; 
team.astCoach = _assistantCoach.text; 

player.firstName = cell.playerFirstName.text; 
player.lastName = cell.playerLastName.text; 
player.number = cell.playerNumber.text; 

[self.team addPlayers:_tempSet]; 

[self.managedObjectContext save:nil]; 
[self.navigationController popViewControllerAnimated:YES];  
} 
////////////////////////////////////////////////////////////////////////////////////////////////// 


-(void)addPlayerButton { 

player = (Player *)[NSEntityDescription insertNewObjectForEntityForName:@"Player" 
                  inManagedObjectContext:self.managedObjectContext]; 

[_tempSet addObject:player]; 

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];  
} 

답변

0

추가 ... 일부 코드 당신의 NewTeamViewController 텍스트 필드의 각 제어 이벤트 UIControlEventEditingChanged의 대상으로. 이것을 코드 (cellForRowAtIndexPath...) 또는 펜촉이나 스토리 보드에서 할 수 있습니다. 각 셀의 세 가지 다른 텍스트 필드 각각에 대해 다른 동작 방법을 사용합니다. 또한,

// Helper method 
- (Player *)playerForTextField:(UITextField *)textField 
{ 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:textField.superview]; 
    return [_tempArray objectAtIndex:indexPath.row]; 
} 

- (IBAction)firstNameDidChange:(UITextField *)textField 
{ 
    Player *player = [self playerForTextField:textField]; 
    player.firstName = textField.text; 
} 

- (IBAction)lastNameDidChange:(UITextField *)textField 
{ 
    Player *player = [self playerForTextField:textField]; 
    player.lastName = textField.text; 
} 

- (IBAction)numberDidChange:(UITextField *)textField 
{ 
    Player *player = [self playerForTextField:textField]; 
    player.number = textField.text; 
} 

테이블에 선수의 순서를 아는 것이 유용 때문에 _tempSet_tempArray로 변경 : 여기에 귀하의 액션 메소드를 보여주고 있습니다.

+0

안녕하세요. 답변 해 주셔서 감사합니다. 하지만 우선 인터페이스 빌더를 사용하지 않습니다. 둘째, 핵심 데이터가 배열이 아닌 데이터를 저장하기 위해 NSSet을 사용해야하기 때문에 _TempSet을 _tempArray로 변경할 수 없습니다 ... – Luke

관련 문제