Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a tableview set up with data from a mutable array but I cannot figure out how to reload it, or even if it's being loaded in the first place.

The mutable array is changed when a user presses a button. When I initialize the array with test data, the table view works fine. I figured that I needed to reload the table, but I've been unsuccessful doing it inside cellForRowAtIndexPath. I've also tried to reload by synthesizing the tableView through the .h and reloading after adding to the array from the add button being clicked method (attempts not reflected in code, but if you think I did it wrong go ahead and post the right way).

There is no error, but nothing happens when I click the add button.

Any help is greatly appreciated.

Relevant code:

.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *company;
- (IBAction)add:(id)sender;


@property (nonatomic, retain) NSMutableArray *currentNames;
@property (weak, nonatomic) IBOutlet UITableView *preExisting;
@end

.m:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
{
    NSMutableArray *currentNames;
    NSMutableArray *currentCompanies;
    NSMutableArray *names;
    NSMutableArray *companies;
}

@synthesize name, company;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSMutableArray *currentNames = [[NSMutableArray alloc] init];
    NSMutableArray *currentCompanies = [[NSMutableArray alloc] init];
    NSMutableArray *names = [[NSMutableArray alloc] init];
    NSMutableArray *companies = [[NSMutableArray alloc] init];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    this.tableView = tableView;
    return [currentNames count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView         dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    cell.textLabel.text = [currentNames objectAtIndex:indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView reloadData];
}


- (IBAction)add:(id)sender {
    BOOL exists = [names containsObject:name.text];

    if(exists == FALSE){
        [currentNames addObject:[name text]];
        [currentCompanies addObject:[company text]];
        [names addObject:[name text]];
        [companies addObject:[company text]];
    }
    else{
        [currentNames addObject:[name text]];
        [currentCompanies addObject:[company text]];
    }

    [currentNames addObject:[name text]];

    name.text=@"";
    company.text=@"";

}

@end
share|improve this question

1 Answer

TableView uses data source (NSArrays or NSMutableArrays) to populate itself. It uses various delegate methods to know how its going to be populated. So in your case, you are using your currentNames, an instance of NSMutableArray to do so. When you manipulate the data inside this array, you would want the TableView to show the now edited array.

So to reload your table view after you are done editing the array in your button's action method, you should call this method

[self.tableView reloadData]

there.

Note :- You would want your data source array to have a scope till your view controller lasts so if you have created a NSMutableArray property in your Interface file, do not recreate an instance of NSMutableArray of the same name in your Implementation file (do not ignore compiler warnings).

In your viewDidLoad method, use

self.currentNames = [[NSMutableArray alloc] init];

Although it will create a memory leak by the way but seriously, go through TableView's documentation thoroughly before you move ahead.

share|improve this answer
Thank you! I've tried the reload in my add action method, but hopefully with these initialize fixes it should work. I'll get back to you after reading through more of the docs and trying it! – NetworkIntern yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.