I've working on a program and I'm having issues getting a tableView to update. Pretty much I have an tableView and there is nothing but empty rows at first. Then the user hits the + sign and I PUSH a new tableview on top of the stack and the user can select from a master list of which rows they want to add to the initial table. The rows that the user picks are added to an array. When the user clicks the back button, the first table doesn't update. However, if I press the HOME key and then reload the app, the first table view displays the updated array. I have included the code from both tables and my gloabl array.
.m File for first table
@interface baseTableViewViewController ()
@end
@implementation baseTableViewViewController
//@synthesize tableView = _tableView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self; }
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)viewDidlAppear:(BOOL)animated{
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated. }
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [[[DataClass getInstance]allItems]count]; }
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"base"];
if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"base"];
}
BaseInfo *infoAtIndex = [[[DataClass getInstance] allItems] objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[infoAtIndex name]];
[[cell detailTextLabel] setText:[infoAtIndex icao]];
return cell; }
-(void)awakeFromNib{
[super awakeFromNib]; }
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
[[self tableView]reloadData];
}
@end
.h file for first table
@class baseTableViewViewController; @class BaseInfoDataController;
@interface baseTableViewViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UITableView *tableView;
@end
.m file for second table (the master list)
@class BaseInfo; @interface MasterListTableViewController ()
@end
@implementation MasterListTableViewController @synthesize delegate;
- (id)initWithStyle:(UITableViewStyle)style {
self = [super initWithStyle:style];
if (self) {
}
return self; }
- (void)viewDidLoad {
[super viewDidLoad]; }
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated. }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.dataController countOfList]; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Base";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
BaseInfo *infoAtIndex = [self.dataController objectInListAtIndex:indexPath.row];
[[cell textLabel] setText:infoAtIndex.name];
[[cell detailTextLabel] setText:infoAtIndex.icao];
return cell; }
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSMutableArray *addBaseList = [[NSMutableArray alloc]init];
self.masterList = addBaseList;
BaseInfo *inform;
if ([selectedCell accessoryType] == UITableViewCellAccessoryNone){
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
inform = [self.dataController objectInListAtIndex:indexPath.row];
NSLog(@"%@",inform.name);
NSLog(@"%@",inform.icao);
[DataClass.getInstance addObject:inform];
}else{
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
-(void)awakeFromNib{
[super awakeFromNib];
self.dataController = [[MasterListDataController alloc]init]; }
-(IBAction)cancel:(id)sender{
[self masterListTableViewControllerDidCancel:self];
}
-(IBAction)done:(id)sender{
[self masterListTableViewControllerDidSave:self]; }
-(void)masterListTableViewControllerDidCancel:(MasterListTableViewController
*)controller{
[self dismissViewControllerAnimated:YES completion:nil]; }
-(void)masterListTableViewControllerDidSave:(MasterListTableViewController
*)controller{
[self dismissViewControllerAnimated:YES completion:nil]; }
@end
below is the .h for the second table
@class MasterListTableViewController; @class BaseInfo;
@interface MasterListTableViewController : UITableViewController
@property (nonatomic, assign) id delegate; @property (nonatomic, copy) NSMutableArray *masterList; @property (nonatomic, retain) NSArray
*baseNames; @property (nonatomic, retain) NSArray *icao; @property (strong, nonatomic) MasterListDataController *dataController; @property (nonatomic, copy)BaseInfo *inform;
- (IBAction)cancel:(id)sender;
- (IBAction)done:(id)sender;
-(void)masterListTableViewControllerDidCancel:(MasterListTableViewController
*)controller;
-(void)masterListTableViewControllerDidsave:(MasterListTableViewController
*)controller;
@end
and finally the global array that i'm using with the methods
@implementation DataClass @synthesize userBaseList;
+(DataClass *)getInstance{
static DataClass *instance = nil;
//@synchronized(self)
{
//if(instance ==nil)
if(!instance)
{
//instance = [DataClass new];
instance = [[super allocWithZone:nil]init];
}
}
return instance; }
-(void)addObject:(BaseInfo*)info{
[self.userBaseList addObject:info];
}
-(void)removeObjectAtIndex:(NSInteger)atIndex{
[self.userBaseList removeObjectAtIndex:atIndex]; }
+(id)allocWithZone:(NSZone *)zone{
return [self getInstance]; }
-(id)init{
self = [super init];
if (self){
userBaseList = [[NSMutableArray alloc]init];
}
return self; }
-(NSArray *)allItems{
return userBaseList; } @end
I think the solution has to be something very simple that I'm missing, but I just don't see it. I've read through the apple developers guide for uitableview development, but I'm still stuck.