jampez77 jampez77 - 10 months ago 42
Objective-C Question

UITableView objective-c not populating correctly

I am trying display cells in a UITableView but newly added cells are not displayed unless

is used.

Full list

This is the full list:
If I go to the 'New Note' screen and come back this is fine but if this screen is accessed by the side menu the note title 'New' disappears until I click on the 'New Note' button and come back.

Below is the code for this page:

- (void)viewDidLoad {
[super viewDidLoad];

NSMutableArray *leftButtonsArray = [self.navigationItem.leftBarButtonItems mutableCopy];
UIBarButtonItem *newNoteButton = [[UIBarButtonItem alloc] initWithTitle:@"New Note" style:UIBarButtonItemStyleBordered target:self action:@selector(addNote:)];
[leftButtonsArray addObject:newNoteButton];
self.navigationItem.leftBarButtonItems = leftButtonsArray;

[super viewWillAppear:animated];
self.notes = [NSMutableArray array];

ontracAppDelegate *delegate = (ontracAppDelegate*)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *moc = [delegate managedObjectContext];

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Notes"];
[request setReturnsObjectsAsFaults:NO];
NSError *error = nil;

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"updated" ascending:NO];
[request setSortDescriptors:@[sortDescriptor]];

NSArray *results = [moc executeFetchRequest:request error:&error];
NSSortDescriptor *sd = [[NSSortDescriptor alloc] initWithKey:@"updated" ascending:NO];

NSArray *sortDescriptors = [NSArray arrayWithObject:sd];
NSArray *sortedArray = [results sortedArrayUsingDescriptors:sortDescriptors];

NSNumber * NOTEID = @"noteID";
NSString * PACKID = @"pack_id";
NSString * NOTE = @"note";
NSString * CREATED = @"created";
NSString * UPDATED = @"updated";

NSDictionary * dict;

[self.notes removeAllObjects];

for(Notes *note in results){

if(self.dataObject.pack_id == [note.dataObject.dataPack.pack_id integerValue]){
dict = [NSDictionary dictionaryWithObjectsAndKeys:
note.noteID, NOTEID,
note.pack_id, PACKID,
note.note, NOTE,
note.created, CREATED,
note.updated, UPDATED,

if((note.created == nil || note.updated == nil || note.noteID == nil)){
[moc deleteObject:note];
[self.notes addObject:dict];

NSLog(@"mArray %@", self.notes);

[super viewDidDisappear:animated];
[super viewDidAppear:animated];

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return 1;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
NSLog(@"notes count: %lu", (unsigned long)[self.notes count]);
return [self.notes count];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"myCell"];

NSMutableDictionary *d = [self.notes objectAtIndex:indexPath.row];
//NSNumber *noteID = [d valueForKey:@"noteID"];
NSString *note = [d valueForKey:@"note"];
NSString *created = [d valueForKey:@"created"];
NSString *updated = [d valueForKey:@"updated"];

cell.textLabel.text = note;
cell.detailTextLabel.text = [NSString stringWithFormat:@"Date Created: %@ | Last Updated %@", created, updated];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

return cell;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
NSMutableDictionary *d = [self.notes objectAtIndex:indexPath.row];
NSNumber *noteID = [d valueForKey:@"noteID"];
NID = noteID;

ontracSuperClassViewController *viewController = [[ontracNotesViewController alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
NID = [NSNumber numberWithInteger:-1];
ontracSuperClassViewController *viewController = [[ontracNotesViewController alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];

Answer Source

When you fill your array with latest updated data then you have to reload tableview to display that latest data. So, if your self.notes prints updated data properly in viewWillAppear in NSLog then you just need to reload your tableview like,

   [tableView reloadData];

and if you are not getting updated data in self.notes then first solve that and then reload tableview!