0

Here's the process I have currently:

1) Tap Add Button 2) Fill out UITextFields in my modal view controller 3) Tap Save Button 4) Data is added to a UITableViewCell 5) Repeat steps 1 - 3 6) New data is added to second cell, but the data in both cells is from the new input

How do I use the same modal view controller to add new data yet retain it in my previous cell(s) and even view the data when I tap on those cell(s)?

Here is my process for going to the modal view controller and adding data to it:

 -(IBAction)goToModalView:(id)sender
 {
    if (self.educationViewController == nil)
    {
        EducationViewController * temp = [[EducationViewController alloc] initWithNibName:@"EducationViewController" bundle:[NSBundle mainBundle]];
        self.educationViewController = temp;

        _dataArray = [[NSMutableArray alloc] init];
        [_dataArray addObject:temp];
    }
    else
    {
        [self addEducation:sender];
    } 

    [self presentViewController:self.educationViewController animated:YES completion:nil];
}

-(IBAction)addEducation:(id)sender
{
    [_myTableView beginUpdates];
    [_dataArray addObject:self.educationViewController.majorString];
    NSArray * paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[_dataArray count]-1 inSection:0]];
    [_myTableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];
    [_myTableView endUpdates];
}

Here is where I am saving the text in my modal view controller:

 -(IBAction)saveData
 {
     if (_majorTextField.text == nil)
     {
        _majorString = @"";
     }
     else
     {
        _majorString = [[NSString alloc] initWithFormat:@"%@", _majorTextField.text];
        [_majorTextField setText:_majorString];
        NSUserDefaults * majorDefault = [NSUserDefaults standardUserDefaults];
        [majorDefault setObject:_majorString forKey:@"major"];
     }
     // Other text field code here

     [self dismissViewControllerAnimated:YES completion:nil];
 }

Any advice is appreciated! Much thanks!

Luke Irvin
  • 1,179
  • 1
  • 20
  • 39

2 Answers2

0

Instead of adding the data directly to the UITableView, I would add it to an NSMutableArray. I would then use that array as the datasource for the table, and after the save button is pressed, I would call

[_myTableView reloadData]; 
David Brunow
  • 1,299
  • 1
  • 12
  • 13
0

I don't understand what in goToModalView() why you do "[_dataArray addObject:temp];". But leave it along, in addEducation(), you may use

[_myTableView reloadData];

to read the whole table.

TieDad
  • 9,143
  • 5
  • 32
  • 58
  • I'm calling reloadData in viewWillAppear. goToModalView presents my modal view so I may enter the text into the text fields. When the cell is added, I am only displaying one string of text – Luke Irvin Nov 30 '12 at 03:52