As will be seen from my question, I'm a beginner to iOS development. I have looked at several tutorials and several questions but none seem to be covering my case (I'm probably missing something). I am simply trying to add elements to an array from one class, then call the method that reloads the table data from another class. However, when I try to reload the data after calling the relevant method from another class, the table fails to be loaded (no new data seems to be added). In fact, even the previous values of the array in the table seem to be gone.
Class A:
MyClassA.h
@interface MyClassA : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
UITableView *tableView;
NSMutableArray *Elements;
}
@property UITableView *tableView;
@property NSMutableArray *Elements;
-(void) addElement: (NSString *) ElementName;
@end
MyClassA.m
@implementation MyClassA
{
NSString *ElementName[10];
}
- (void) viewWillAppear:(BOOL)animated
{
NSLog(@"MyClassA: viewWillAppear");
ElementName[0] = @" 1 ";
ElementName[1] = @" 2 ";
ElementName[2] = @" 3 ";
Elements = [[NSMutableArray alloc]initWithObjects:ElementName[0], ElementName[1], ElementName[2],nil];
self.tableView.dataSource = self; // The table successfully loads with the data element
}
-(void) addElement: (NSString *) ElementName
{
NSLog(@"Entered addElement"); // This method is successfully accessed
Elements = [[NSMutableArray alloc]initWithObjects:ElementName[0], ElementName[1], ElementName[2],nil]; // The problem is here, printing data in this array shows they have no value
[self.tableView reloadData]; // The problem is here. This does not load the data
}
Class B:
MyClassB.h
@class MyClassA
@interface MyClassB : UIViewController
@property (nonatomic, strong) MyClassA *MyClassACall
-(IBAction) MyButtonClicked: (id) sender;
@end
MyClassB.m
@implementation MyClassB
@synthesize MyClassACall;
-(id) init
{
self = [super init]
if (self) {
NSLog(@"MyClassB init");
MyClassACall = [[MyClassA alloc] init];
}
return self;
}
-(IBAction)MyButtonClicked:(id)sender
{
NSLog("My button is clicked");
[self.MyClassAcall addElement:@"NewElement"];
}
I successfully get all the NSlogs, and the table loads the first time correctly with the provided data. However, when trying to add data by loading a method from another class, the table data remains the same and does not get reloaded. What am I missing here?
I have not loaded the whole code to keep this simple. I hope that this makes sense and please let me know if I could clarify. If a similar question exists, please point me to it and I appreciate your help.
See Question&Answers more detail:os