Ios 在核心数据中保存tableview单元格中的数据
嗨,我正在尝试将数据保存到核心数据中,但遇到了一些问题。。。我有一个团队实体和一个玩家实体。团队实体与玩家实体建立一对多关系。。。在我的“NewTeamViewController”中有两个部分,第二部分是向团队添加球员的部分。在部分标题中有一个按钮,用于添加新玩家。。。当按下该按钮时,会出现一个新的单元格,其中有三个文本字段,每个字段中都有默认文本(不是占位符文本),然后我将新玩家添加到一个可变表集中,该可变表集将作为团队玩家添加。tableview使用自定义单元格(这是三个文本字段所在的位置) 团队保存正确,但我无法保存播放器单元格中三个文本字段的数据。它只是将默认文本保存在播放器的单元格中 我不确定如何或在何处将新添加的单元格中的数据提供给新添加的播放器对象。 这里有一些代码Ios 在核心数据中保存tableview单元格中的数据,ios,uitableview,core-data,uitextfield,custom-cell,Ios,Uitableview,Core Data,Uitextfield,Custom Cell,嗨,我正在尝试将数据保存到核心数据中,但遇到了一些问题。。。我有一个团队实体和一个玩家实体。团队实体与玩家实体建立一对多关系。。。在我的“NewTeamViewController”中有两个部分,第二部分是向团队添加球员的部分。在部分标题中有一个按钮,用于添加新玩家。。。当按下该按钮时,会出现一个新的单元格,其中有三个文本字段,每个字段中都有默认文本(不是占位符文本),然后我将新玩家添加到一个可变表集中,该可变表集将作为团队玩家添加。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];
}
将您的
NewTeamViewController
添加为每个文本字段的控件事件UIControlEventEditingChanged
的目标。您可以在代码(cellforrowatinexpath…
)或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;
}
另外,将
\u tempSet
更改为\u tempArray
,因为了解表中玩家的顺序很有用 嗨,谢谢你的回答。但首先,我没有使用界面生成器。其次,我不能将_TempSet更改为_tempArray,因为核心数据需要使用NSSet来存储数据,而不是数组。。。