0

I want to populate a UITableView by passing a string from a UIViewController to a UITableView.

This is my code so far:

UITableViewController.m

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        NSMArray = [[NSMutableArray alloc]initWithObjects:@"text",@"text2", nil];
    }

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return NSMArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier ];
    }
    cell.textLabel.text = [NSMArray objectAtIndex:indexPath.row];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //remove fron NSMutableArray
        [NSMArray removeObjectAtIndex:indexPath.row];
        //delete from table
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }
}
    -(void)getString:(NSString *)string
    {
        if ([make length] > 0) {

            if (!NSMArray)
            {
                NSMArray = [[NSMutableArray alloc]init];
            }
            [NSMArray insertObject:make atIndex:0];
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
            [self.myTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }

UITableViewController.h

    #import <UIKit/UIKit.h>
    #import "ViewController.h"

    @interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
    @property (strong, nonatomic) IBOutlet UITableView *myTableView;
    @property (strong, nonatomic) NSMutableArray *NSMArray;
    -(void)getString:(NSString *)string;

ViewController.m

#import "entryViewController.h"

@interface entryViewController ()

@end

@implementation entryViewController
@synthesize textfieldString, myString;

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Save button
    UIBarButtonItem *SaveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(pushCarMake:)];
    self.navigationItem.rightBarButtonItem = SaveButton;
}

- (void)pushCarMake:(id)sender
{

    myString = textfieldString.text;
    ViewController *mainVC = [[ViewController alloc]init];
    [mainVC getString:myString];
    //pop to root ViewController
    [self.navigationController popToRootViewControllerAnimated:TRUE];

}
@end

ViewCotroller.h

#import <UIKit/UIKit.h>
#import "ViewController.h"

@interface entryViewController : UIViewController

@property (strong, nonatomic) NSString *carMake;
@property (strong, nonatomic) IBOutlet UITextField *textfieldCarMake;

So this works fine and populates the Table View with the string items instantiated in the viewDidLoad of the UITableViewCotroller. But every time I pass a string from the ViewControllers to the table View it does not go into the table. any suggestions why this is. Sorry for pasting so much code.

arled
  • 2,577
  • 6
  • 27
  • 49

2 Answers2

1

One approach for passing data from a detail view controller back to it's parent is using a delegate. Here's an example:

How do I set up a simple delegate to communicate between two view controllers?

[Edited]

The other approach if you're using storyboards is to use an unwind segue. Here's a sample I put together using this approach: github.com/soleares/AddToTableView

Community
  • 1
  • 1
Jesse
  • 1,667
  • 12
  • 16
  • This is from 2011 I'm sure theres a better way now – arled Jan 14 '14 at 23:27
  • @develEnv Yes, there's another approach if you're using storyboards called unwind segues. See https://developer.apple.com/library/ios/technotes/tn2298/_index.html. Those are your two options and they both work well. You should make yourself familiar with delegates because they're an essential part of iOS development. – Jesse Jan 14 '14 at 23:31
  • thanks Jesse, Yes I am using storyboards I'll have a look at that now – arled Jan 15 '14 at 00:07
  • @develEnv Here's a good tutorial http://pragmaticstudio.com/blog/2013/2/5/unwind-segues. Make sure to create the unwind action method before you wire it up in the storyboard otherwise it won't show up when you drag to 'Exit'. That was the most confusing part for me when I first set one up. The tutorial does it in this order so if you follow it you should be fine. – Jesse Jan 15 '14 at 00:12
  • Still cant get it to work. I though it would be a simple thing. But that does not seem to be the case – arled Jan 15 '14 at 00:42
  • @develEnv see the sample I added above. – Jesse Jan 15 '14 at 05:24
  • Thanks Jesse I'll have a look at that :) – arled Jan 15 '14 at 11:48
  • one last thing pls. where do u set this [sender.identifier isEqualToString:@"dismissAddViewController"]? – arled Jan 15 '14 at 20:45
  • In Interface Builder you'll see an unwind segue listed in the Add View Controller. Select it and in the Attribute inspector on the right you can set the Identifier. – Jesse Jan 15 '14 at 21:31
0

Not positive if this is the only issue, but try adding beginUpdates and endUpdate

[myTableView beginUpdates];
[self.myTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[myTableView endUpdates];
ansible
  • 3,569
  • 2
  • 18
  • 29