Automatically Reload TableViewController On Rewind

2019-07-22 13:21发布

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.

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.

3条回答
小情绪 Triste *
2楼-- · 2019-07-22 13:59

Try reloading your tabledata in viewWillAppear in initial (tableview)controller.

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

Or call again the function through which you are loading your data from Realm like

override func viewWillAppear(animated: Bool) {
         getMyData() //or whatever your function name is
 }
查看更多
可以哭但决不认输i
3楼-- · 2019-07-22 14:06

You can use NSNotification for that.

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.

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 it will help

查看更多
We Are One
4楼-- · 2019-07-22 14:10

If you are using storyboard unwind segue, try using

func unwindSegue(segue:UIStoryboardSegue) {
    if segue.identifier == "identifier" {
        getData()
        self.tableView.reloaddata()
    }
}
查看更多
登录 后发表回答