Update the data of a TableViewController after add item
Asked Answered
N

1

2

I have tableView (A) responsible for listing items CoreData and viewController (B) to add new item. To create a ViewController (B) custom, they chose to use the following Present Modally to use presentation Over Current Context to enable transparent background and be able to apply the blur.

  • From tableView (A) press the button to add a new item, the viewController (B) appears.
  • Add a new item and saved in CoreData
  • The viewController (B) execute self.dismissViewControllerAnimated(true, completion: nil) and the display returns to the tableView (B)

The problem is that the table view does not update the data.

Following a post from Stack Overflow resulting in code:

override func viewDidAppear(animated: Bool) {
    self.loadData()
}

But at the back of viewController (B), nothing happens in Xcode does not pass the lines of viewDidAppear.

Can anyone help me?

Neves answered 16/10, 2015 at 2:7 Comment(6)
I read your question there times, and I still do not understand your question.Charily
You should use an NSFetchedResultsController to populate your tableView. If you use its delegate methods (there is boilerplate code in the Apple docs), it will automatically update the TV as soon as you add the new object.Tiaratibbetts
@Tiaratibbetts The problem is not in the loading data in tableView, is in the back of a sigue (ViewController to add an item to CoreData)Neves
@Neves Yes. As I understand it, you want the tableView in vc A updated when vc B is dismissed. But if you use NSFetchedResultsController in vc A, you do not need to rely on lifecycle methods like viewDidAppear to trigger the update - the FRC will do it for you.Tiaratibbetts
@Tiaratibbetts You could post an answer with an example of use of NSFetchedResultsController?Neves
@Neves Sure - if you edit your post to include your current tableView methods, I can work up an answer based on FRCTiaratibbetts
E
2

Try this:

 override func viewDidAppear(animated: Bool)
{
    self.yourTableViewName.reloadData()
}
Equalitarian answered 8/12, 2015 at 11:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.