ios 如何快速从另一个视图控制器重新加载 tableview
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25921623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reload tableview from another view controller in swift
提问by Suneet Tipirneni
When I dismiss a modal view controller I want the tableview to update, I am using the form sheet presentation style on iPad so the viewWillAppear
and the viewDidAppear
methods will not work
当我关闭模态视图控制器时,我希望 tableview 更新,我在 iPad 上使用表单演示样式,因此viewWillAppear
这些viewDidAppear
方法将不起作用
采纳答案by Rafael
I find the segue approach more elegant.
我发现 segue 方法更优雅。
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
.
假设我们有ViewControllerA
和 a ViewControllerB
。我们在ViewControllerB
,我们想从ViewControllerB
直接跳回ViewControllerA
并更新ViewControllerA
.
In ViewControllerA
add the following action in your ViewController class:
在ViewControllerA
您的 ViewController 类中添加以下操作:
@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!
是的,这段代码在你想要返回的 ViewController 的 ViewController 中!
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:
现在,您需要从ViewControllerB
的故事板 ( StoryboardB
)创建退出转场。继续并打开StoryboardB
并选择故事板。按住 CTRL 并拖动退出,如下所示:
You will be given a list of segues to choose from including the one we just created:
您将获得一个 segue 列表供您选择,包括我们刚刚创建的一个:
You should now have a segue, click on it:
你现在应该有一个 segue,点击它:
Go in the inspector and set a unique id:
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):
在ViewControllerB
您想要关闭并返回的点ViewControllerA
,执行以下操作(使用我们之前在检查器中设置的 id):
self.performSegue(withIdentifier: "yourIdHere", sender: self)
Now, whenever you use the segue to return back to ViewControllerA
, the ViewControllerA
will update the TableView straightaway.
现在,每当您使用 segue 返回到 时ViewControllerA
,ViewControllerA
都会立即更新 TableView。
回答by Sebastian
You can do this:
你可以这样做:
In your tableView Controller:
在您的 tableView 控制器中:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(loadList), name: NSNotification.Name(rawValue: "load"), object: nil)
}
func loadList(notification: NSNotification){
//load data here
self.tableView.reloadData()
}
Then in the other ViewController :
然后在另一个 ViewController 中:
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
回答by mriaz0011
Swift 3 version code: In your first view controller:
Swift 3 版本代码:在你的第一个视图控制器中:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(loadList), name: NSNotification.Name(rawValue: "load"), object: nil)
}
func loadList(){
//load data here
self.tableView.reloadData()
}
In your second view controller:
在您的第二个视图控制器中:
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
回答by kauai
Missing comma on this line, should instead be:
此行缺少逗号,应改为:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadList:", name:"load", object: nil)
回答by Nicolas Miari
An alternate solution:override UIViewController
's dismiss(animated:completion:)
method on the view controller that manages the table view (and presents the other one modally), so you can reload the table then:
另一种解决方案:在管理表视图的视图控制器上覆盖UIViewController
的dismiss(animated:completion:)
方法(并以模态方式呈现另一个),因此您可以重新加载表:
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
super.dismiss(animated: flag, completion: completion)
self.tableView.reloadData()
}
Note:This should work even if you call dismiss(animated:completion:) on the modal view controller itself (a viable alternative), because ultimately the call gets relayed to the presentingview controller.
注意:即使您在模态视图控制器本身(一个可行的替代方案)上调用dismiss(animated:completion:),这也应该有效,因为最终调用会被中继到呈现视图控制器。
From the method's docs:
从该方法的文档:
The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presentedview controller itself, UIKit asks the presentingview controller to handle the dismissal.
呈现视图控制器负责解除它呈现的视图控制器。如果您在呈现的视图控制器本身上调用此方法,UIKit 会要求呈现的视图控制器处理关闭。
(emphasis mine)
(强调我的)
回答by Deep
You can use NotificationCenter to update your tableview.
您可以使用 NotificationCenter 更新您的 tableview。
First add observer...
首先添加观察者...
NotificationCenter.default.addObserver(self, selector: #selector(doThisWhenNotify(notification:)), name: NSNotification.Name(rawValue: "load"), object: nil)
func doThisWhenNotify(notification : NSNotification) {
let info = notificatio.userInfo
//update tableview
}
Post on other ViewController
在其他 ViewController 上发布
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil, userInfo: [String : Any])