简体   繁体   中英

Saving data from tableview cells in core data

Hi I am trying to save data into core data and having some trouble... I have a Team Entity and a Player Entity The Team Entity is set up as a one to many relationship with the Player Entity... on my "NewTeamViewController" there are two sections, in the second section is where you add players to the team. In the section header there is a button to add a new player... When that button is pressed a new cell appears with three textFields, each with default text in them (not placeholder text) then I am adding the new player to a MutableSet that will be added as the teams players. The tableview is using a custom cell (which is where the three textFields live)

The team saves correctly, except I am unable to save the data from the three text fields in the player cell. It just saves the default text in the cell for the player.

I am not sure how or where to give the data from the newly added cell to the newly added player object.

Here is some code...

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

Add your NewTeamViewController as a target for the control event UIControlEventEditingChanged for each of your text fields. You can do this in code ( cellForRowAtIndexPath... ) or in your nib or storyboard. I would use a different action method for each of the three different text fields in each cell. Here is what your action methods might look like:

// 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;
}

Also, change your _tempSet to a _tempArray because knowing the order of the players in the table is useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM