Reload tableView after dismiss a viewController

前端 未结 5 957
遇见更好的自我
遇见更好的自我 2021-02-14 14:05

I have a ViewController(VCA) with a TableView inside. From this ViewController it is possibile to call another ViewController (VCB). In this second VC it is possibile add an ite

相关标签:
5条回答
  • 2021-02-14 14:36

    The code in your question is perfectly good. I use the same approach and it works like a charm. IMHO the problem in your code is that you only refresh the table view with self.tableView.reloadData(), BUT may be you forget to refresh your data model - the data source for the table view. E.g. if you delete an entity from Core Data then you need to refetch your data and only after that reload the table view.

    0 讨论(0)
  • 2021-02-14 14:47

    you can do this in two way :-

    1)
    Do One thing in VCA

    VCA

       override func viewWillAppear(_ animated: Bool){
           tableView.reloadData()
       }
    

    If this does not work out then try this.

    2) create an instance of VCA in VCB and whenever you move from VCA to VCB pass the value of VCA to the instance of VCB and from there reload the table.

    VCB

        var instanceOfVCA:VCA!    // Create an instance of VCA in VCB
    
       func saveButton(){
        instanceOfVCA.tableView.reloadData()  // reload the table of VCA from the instance
        dismiss(animated: true, completion: nil) 
      }
    

    VCA

     override func prepare(for segue: UIStoryboardSegue, sender: Any!) {
      VCB.instanceOfVCA = self   // Pass the value of VCA in instance of VCB while navigating through segue
    }
    
    0 讨论(0)
  • 2021-02-14 14:50

    Try this: make changes as below

    let addItemVC : VCB? = nil
    

    In ViewDidLoad

     override func viewDidLoad() {
                super.viewDidLoad()
                addItemVC = (storyboard?.instantiateViewController(withIdentifier: "ViewControllerID") as! SelectionViewController?)! // change ViewControllerID with your controller id
                addItemVC.delegate = self
    
        }
    
    0 讨论(0)
  • 2021-02-14 15:00

    Here you are calling table's reload data when the viewcontroller is not yet shown. i.e, Even before you dismissed the viewcontroler VCB and viewcontroller VCA is shown, you are calling reloadData. Try calling reload data in VCA's viewWillAppear(animated: Bool) function.

    0 讨论(0)
  • 2021-02-14 15:00

    I managed to do it using delegate/protocol that is usually used to pass data between view controllers but in this instance I just called the function without passing data and inside this function i put ( tableView.reloadData() ) and it worked like a sweet :)

    juts google "Passing data between view controllers and use the method as I explained above"

    0 讨论(0)
提交回复
热议问题