0

Basically I have two View Controllers: a master and a detail. I'd like for a string inputted in the detail controller to be displayed in a table in the master. In the master controller

I have:

  • a NSMutableArray property called names
  • a prepareForSegque method that delegates back
  • a method to get the string from the 2nd controller:

    -(void) detailViewControllerAdd:(DetailViewController *)detailViewController{

        [self.names addObject:detailViewController.name];
        [self dismissViewControllerAnimated:YES completion:nil];    }
    
  • and a UITableView:

    -(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath{
    
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
          cell.textLabel.text = self.names[indexPath.row];
          return cell;
    }
    
    -(NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) section
    {
          return self.names.count;
    }
    

In my second ViewController (detail) I have a textfield and an "add" button. I'd like for the textfield text to be added into the table rows of the master view controller.

-(IBAction)addButton: (id)sender
{
     self.name = self.nameTextField.text;
     [self.delegate detailViewControllerAdd:self];
}

Sorry if that's confusing, I'm trying to keep it as simple as possible. I know the names are being added fine because I can see them in NSLog. I've also tried looking up people's suggestions but [tableView reloadData] doesn't work at all, and [self.tableView] doesn't even exist.

Any help or advice is greatly appreciated. Also, the segue action is 'modal' if that helps.

Adam Eberbach
  • 12,309
  • 6
  • 62
  • 114
Julia
  • 65
  • 1
  • 1
  • 9
  • The problem is the way you load the table. Try to search how to display an UITableView or you can see the link: http://www.codigator.com/tutorials/ios-uitableview-tutorial-for-beginners-part-1/ – sahara108 Mar 27 '14 at 04:50
  • Thank you sahara108, that link really helped and it's all good now :) – Julia Mar 27 '14 at 08:19

2 Answers2

0

Please see this very popular question. showing passing data between view controllers.Surely it will help.

Community
  • 1
  • 1
jnix
  • 480
  • 3
  • 10
0

In cellForRow, you should create cell if don't have a reusable cell.

-(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath{

      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
      if (cell == nil) {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
      }

      cell.textLabel.text = self.names[indexPath.row];
      return cell;
}

if you don't want to check cell == nil, you should use - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath which is available above iOS6.

simalone
  • 2,768
  • 1
  • 15
  • 20