在倒带时自动重新加载TableViewController [英] Automatically Reload TableViewController On Rewind
问题描述
我正在开发一个应用程序,它从一个tableViewController开始,它加载并显示存储在Realm数据库中的数据。我有它所以我可以在一个单独的场景中在我的Realm数据库中创建一个新条目,并且保存按钮展开会回到初始tableView。
I am working on an app where it starts out at a tableViewController which loads and displays data stored in a Realm database. I have it so I can create a new entry in my Realm database in a separate scene and the save button unwind segues back to the initial tableView.
我当前有它所以tableViewController会在下拉时重新加载tableView(我在这里学到的东西,第二个回答)但是如果tableView在展开时自动重新加载它自己,我会更好,显示我的数据库中的所有数据,包括我的新条目。有人可以指导我一个教程,教我如何做到这一点。
I current have it so the tableViewController will reload the tableView on pull down (something I Learned here, second answer down) but I would be better if the tableView would reload its self automatically upon unwind, displaying all the data in my database, including my new entry. Could someone please direct me to a tutorial that will teach me how this is done.
其他信息:我的应用程序嵌入在导航控制器中。保存按钮位于底部工具栏。
Additional info: My app is embedded in a navigation controller. The save button is located the bottom tool bar.
推荐答案
您可以使用 NSNotification
为此。
首先在 tableViewController
中将此添加到 viewDidLoad中
方法:
First of all in your tableViewController
add this in your viewDidLoad
method:
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "refreshTable:", name: "refresh", object: nil)
}
这会调用你班上的一个方法:
And this will call one method from your class:
func refreshTable(notification: NSNotification) {
println("Received Notification")
tableView.reloadData() //reload your tableview here
}
所以也要添加这个方法。
So add this method too.
现在在你的下一个视图控制器中添加新的数据到数据库中添加这个 unWindSegue
功能:
Now in your next view controller where you add new data into data base add this in you unWindSegue
function:
NSNotificationCenter.defaultCenter().postNotificationName("refresh", object: nil, userInfo: nil)
Hope它会帮助
这篇关于在倒带时自动重新加载TableViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!