Search code examples
uitableviewswiftios8refreshreload

I can't get my UITableView to reload data in swift


I'm using a UITableView to list a number of settings with the state of that setting displayed in the cell title. When the cell is selected it segues to another view where I change the setting. I use a navigation control to go back to the UITableView however I'm unable to update the table

class OptionTable: UITableViewController{

var table : [[String:AnyObject]] = []
var data = NSMutableDictionary()

func dataFilePath() -> String {
    let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
    let documentsDirectory = paths[0] as NSString
    return documentsDirectory.stringByAppendingPathComponent("data.plist") as String
}

override func viewDidLoad() {
    super.viewDidLoad()
    data = NSMutableDictionary(contentsOfFile: self.dataFilePath())!
    table = data["table"] as [[String:AnyObject]] 
}
override func viewWillAppear(animated: Bool) {

    self.tableView.reloadData()

}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return table.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var rowTable : Dictionary = table[indexPath.row]
        cell.textLabel?.text = rowTable["Title"] as? String

        return cell
    }
}

The data is getting saved in the plist its just not being reloaded when I open the UITableView back up. The table does get reloaded when I go back another page then forwards again to the UITableView.

Why is self.tableView.reloadData() not working??? Any help would be greatly appreciated.


Solution

  • You may be saving the to the plist, but viewDidAppear is not loading it before refreshing the tableView.

    Move the loading of the plist into viewDidAppear.