How to Reload Data in a Tableview from a Different Viewcontroller in Swift

reload tableView from Another ViewController Swift 4.2

Thanks for every one.Finally after a R&D. i found the following solution. and its working fine.

let fav:FavoritesFiltersViewController! 
fav.reloadData()

How to reload tableview from another view controller in swift

I find the segue approach more elegant.

Let's say that we have ViewControllerA and a ViewControllerB. We are at ViewControllerB and we want from ViewControllerB to jump straight back to ViewControllerA and update the table view in ViewControllerA.

In ViewControllerA add the following action in your ViewController class:

@IBAction func unwindToViewControllerA(segue: UIStoryboardSegue) {
DispatchQueue.global(qos: .userInitiated).async {
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}

Yes, this code goes in the ViewController of the ViewController you want to go back to!

Now, you need to create an exit segue from the ViewControllerB's storyboard (StoryboardB). Go ahead and open StoryboardB and select the storyboard. Hold CTRL down and drag to exit as follows:

Sample Image

You will be given a list of segues to choose from including the one we just created:

Sample Image

You should now have a segue, click on it:

Sample Image

Go in the inspector and set a unique id:
Sample Image

In the ViewControllerB at the point where you want to dismiss and return back to ViewControllerA, do the following (with the id we set in the inspector previously):

self.performSegue(withIdentifier: "yourIdHere", sender: self)

Now, whenever you use the segue to return back to ViewControllerA, the ViewControllerA will update the TableView straightaway.

Reload UITableView after changing its underlying data in a different View Controller in Swift 5?

In order to update the table View with the new content you have to call this method:
tableView.reloadData()

There are two ways of updating the tableView when making changes inside another View Controller.

First: Initialise or pass in the reference to the tableView to the new view controller. Then you are able to call tableView.reloadData()out of your second VC as soon as you made the changes.

However the second solution is the one you should aim for.

Second: Use the Delegate Design Pattern to inform the first View Controller as soon as the second View Controller will Disappear or the changes are made:

protocol SubviewDelegate {
func subviewWillClose()
}

class SecondVC: UIViewController {

var delegate: SubviewDelegate?

override viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
delegate?.subviewWillClose()
}

}

Now, your first View Controller must conform to the SubviewDelegate Protocol and implement the method subviewWillClose:

extension FirstVC: SubviewDelegate {

func subviewWillClose() {
tableView.reloadData()
}

}

And don't forget to setup the delegate property with an instance of the FirstVC Class, so that its not nil!

Hope this helps you!

How to save data from another view controller and reload tableview?

You tableview in startingViewController is nil this is the problem. When you call the MainVC.getAllItems() your tabview is not initialized. Probably you are re creating startingViewController on your second controller to reach it getAllItems function but it is a wrong approach. You need to update previous viewController datas with protocols or notifications.

First you need to create a protocol like below

protocol AddViewControllerDelegate {
func updateTableView()
}

After that you need to define a variable in your addViewcontroller with this protocol type and call protocol's function when user adds new expense.

class AddViewController: UIViewController {

var delegate: AddViewControllerDelegate?

func callUpdateTableView() {
delegate?.updateTableView()
}
}

In your StartingViewController must conform this protocol. So you need to add updateTableView function. Also you need to say the delegate of your second class is your first class in where you show your addViewController.

class StartingViewController: UIViewController, AddViewControllerDelegate {

func goToAddViewController() {
let vc = AddViewController()
vc.delegate = self
show(vc, sender: nil)
}

func updateTableView() {
// Reload Tableview
}
}

So basically, when you call the protocol function from your secondViewController, your firstViewController's updateTableView function called and you can reload your tableview in this function.



Related Topics



Leave a reply



Submit