简体   繁体   中英

How to add items to TableView without Restarting the Simulator

In my app I have a table view controller with two buttons. One adds an item, and the other edits the table view. When I add an item, the console says that it has added an item, but it's not until i restart the simulator to see that items are being added to the table view. When I add an item I make sure to reload the tableview like so:

- (IBAction)addNewItem:(id)sender

{
NSManagedObjectContext *context = [self managedObjectContext];
Item *itemData = [NSEntityDescription insertNewObjectForEntityForName:@"Item" inManagedObjectContext:context];
[itemData setValue:userText.text forKey:@"name"];

NSError *error;

if (![context save:&error])
{
    NSLog(@"Couldnt find the save %@", error.localizedDescription);
}

else
{
    NSLog(@"It saved properly");
}

[self.tableView reloadData];

}

I just don't understand what the problem could be...

All help is appreciated,

Thanks in advance

The tableView keep to show the old fetch result after the new item has been added. You need to fetch the data again after item has been added and then call the [self.tableView reloadData] method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM