I am using a UISplitviewController and I am trying to add items to the table view.
right now I have two ways
Create a button and add it:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
and this code runs when the button is clicked:
- (void)insertNewObject:(id)sender {
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
and adds an item to the table view:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSString *object = self.objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
this way works.
This is what I am trying to do:
- (void)GetRequest
{
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
but this does not update the table view. I added a breakpoint and when I use the button I added, it goes into the table view, with the method it does not and I am calling the method via [MasterController GetRequest];
What am I doing wrong ?
I am calling GetRequest from another controller.
This is how MasterController is getting defined:
@interface DetailController ()
{
MasterController *MasterController;
}
DetailController.m:
#import "MasterController.h"
@interface DetailController ()
{
MasterController *MasterController;
}
@end
@implementation DetailController
-(void)viewDidLoad {
MasterController = [[MasterController alloc]init];
}
[MasterController GetRequest];
MasterController.m:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSString *object = self.objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
- (void)GetRequest
{
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects insertObject:[NSDate date] atIndex:0];
[self.tableView reloadData];
}
As I suspected, with this line, MasterController = [[MasterController alloc]init]
, you're creating a new instance that has nothing to do with the one you see on screen. You need to get a reference to the master controller that you already have in the split view controller. From the detail view controller, you can get that like so,
MasterController = self.splitViewController.viewControllers.firstObject;
The split view controller has a viewControllers property, and the one at index 0 is the master, and the one at index 1 is the detail. BTW, you should start your ivars and method names with a lower class letter.