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