简体   繁体   English

关闭 viewController 后重新加载 tableView

[英]Reload tableView after dismiss a viewController

I have a ViewController(VCA) with a TableView inside.我有一个 ViewController(VCA),里面有一个 TableView。 From this ViewController it is possibile to call another ViewController (VCB).从这个 ViewController 可以调用另一个 ViewController (VCB)。 In this second VC it is possibile add an item to the plist used to populate the TableView in VCA.在这第二个 VC 中,可以向用于填充 VCA 中的 TableView 的 plist 添加一个项目。 The problem is that when I save the new item and dismiss the VCB, I can't reload the TableView in VCA.问题是当我保存新项目并关闭 VCB 时,我无法在 VCA 中重新加载 TableView。

I have found a lot of examples:我找到了很多例子:

How can you reload a ViewController after dismissing a modally presented view controller in Swift? 在 Swift 中关闭模态呈现的视图控制器后,如何重新加载 ViewController?

How to call reload table view after you call dismissViewController in swift? 在swift中调用dismissViewController后如何调用重新加载表视图?

How to reload tableview from another view controller in swift 如何快速从另一个视图控制器重新加载 tableview

Update the data of a TableViewController after add item 添加项目后更新TableViewController的数据

Update the first view after dismissing Popover 关闭 Popover 后更新第一个视图

Table view is not getting updated after dismissing the popover? 关闭弹出窗口后,表视图没有更新?

after reading i tried with this code:阅读后,我尝试使用此代码:

    **IN VCB**

import UIKit 

protocol AddItemDelegateProtocol {
    func didAddItem()
}

class VCB: UIViewController {

    var delegate : AddItemDelegateProtocol?
    ...
}

@IBAction func saveButton(_ sender: Any) {
        .... 
   self.delegate?.didAddItem()
   dismiss(animated: true, completion: nil)     
   }



**In VCA**

class VCA: UIViewController, UITableViewDelegate, UITableViewDataSource, AddItemDelegateProtocol {

let addItemVC = VCB()
...

override func viewDidLoad() {
        super.viewDidLoad()

        addItemVC.delegate = self
        ...
    }

func didAddItem() {
        self.tableView.reloadData()
    }

but this doesn't work.但这不起作用。 I don't understand where I'm wrong.我不明白我错在哪里。 Could you help me?你能帮我吗?

EDIT: my Solution I solved in this way:编辑:我的解决方案是这样解决的:

I've created a singleton in which I declare:我创建了一个单身人士,我在其中声明:

    class DataManager {

        static let shared = DataManager()
        var firstVC = VCA()
.....
}

then, in viewDidLoad of VCA :然后,在 VCA 的 viewDidLoad 中

DataManager.shared.firstVC = self

now, in the saveButton of VCB , i can call:现在,在 VCB 的 saveButton 中,我可以调用:

@IBAction func saveButton(_ sender: Any) {
    ........
    DataManager.shared.firstVC.tableView.reloadData()
    dismiss(animated: true, completion: nil)
}

you can do this in two way :-你可以通过两种方式做到这一点:-

1) 1)
Do One thing in VCA在 VCA 中做一件事

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. 2) 在 VCB 中创建一个 VCA 实例,每当您从 VCA 移动到 VCB 时,将 VCA 的值传递给 VCB 的实例,然后从那里重新加载表。

VCB 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 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
}

Here you are calling table's reload data when the viewcontroller is not yet shown.在这里,当视图控制器尚未显示时,您正在调用表的重新加载数据。 ie, Even before you dismissed the viewcontroler VCB and viewcontroller VCA is shown, you are calling reloadData.即,即使在您关闭 viewcontroler VCB 并显示 viewcontroller VCA 之前,您也正在调用 reloadData。 Try calling reload data in VCA's viewWillAppear(animated: Bool) function.尝试在 VCA 的viewWillAppear(animated: Bool)函数中调用重新加载数据。

Try this: make changes as below试试这个:进行如下更改

let addItemVC : VCB? = nil

In ViewDidLoad在 ViewDidLoad 中

 override func viewDidLoad() {
            super.viewDidLoad()
            addItemVC = (storyboard?.instantiateViewController(withIdentifier: "ViewControllerID") as! SelectionViewController?)! // change ViewControllerID with your controller id
            addItemVC.delegate = self

    }

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.恕我直言,您的代码中的问题是您只使用self.tableView.reloadData()刷新表视图,但您可能忘记刷新数据模型- 表视图的数据源。 Eg if you delete an entity from Core Data then you need to refetch your data and only after that reload the table view.例如,如果您从 Core Data 中删除了一个实体,那么您需要重新获取您的数据,并且只有在重新加载表视图之后。

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 :)我设法使用通常用于在视图控制器之间传递数据的委托/协议来做到这一点,但在这种情况下,我只是在不传递数据的情况下调用了该函数,并在该函数内部放置了( tableView.reloadData() ),它的工作方式很甜蜜:)

juts google "Passing data between view controllers and use the method as I explained above"突出谷歌“在视图控制器之间传递数据并使用我上面解释的方法”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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