2012-07-14 55 views
1

嗨我试图将数据保存到核心数据中,并且遇到了一些麻烦......我有一个团队实体和一个玩家实体团队实体设置为一对多关系玩家实体...在我的“NewTeamViewController”中有两个部分,第二部分是您将玩家添加到团队的位置。在章节标题中有一个按钮来添加一个新的播放器...当按下该按钮时,一个新的单元格将出现三个textField,每个textField都有默认文本(不是占位符文本),然后我将新播放器添加到MutableSet这将作为球队球员加入。桌面视图使用自定义单元格(这是三个文本字段所在的地方)在核心数据中保存来自tableview单元格的数据

团队正确保存,但我无法保存播放器单元格中三个文本字段的数据。它只是将默认文本保存在播放器的单元格中。

我不知道如何或从何处将数据从新添加的单元格添加到新添加的播放器对象。

下面是一些代码...

-(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

嗨,谢谢你的回答。但首先我不使用接口构建器。其次,我无法将_TempSet更改为_tempArray,因为核心数据需要使用NSSet来存储数据而不是阵列... – Luke 2012-07-15 15:12:38