1

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

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

我不确定如何或在何处将新添加的单元格中的数据提供给新添加的播放器对象。

这是一些代码...

-(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];     
}
4

1 回答 1

0

将您添加为每个文本字段NewTeamViewController的控制事件的目标。UIControlEventEditingChanged您可以在代码 ( cellForRowAtIndexPath...) 或您的 nib 或情节提要中执行此操作。我会为每个单元格中的三个不同文本字段中的每一个使用不同的操作方法。以下是您的操作方法可能的样子:

// 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为 a _tempArray,因为了解牌桌中玩家的顺序很有用。

于 2012-07-15T05:35:03.317 回答