I'm working on small CoreData program with one UITableView with embedded Navigation controller and UIViewController, where to add the CoreData.
The problem is that I can't figure out how to reload the data in the table after press the save button.
I have tried to use popToRootViewControllerAnimated(true) in the @IBAction func buttonSavePressed(sender: UIBarButtonItem), but this just send me to the TableView without reloading the data.
@IBAction func buttonSavePressed(sender: UIBarButtonItem) {
self.navigationController?.popToRootViewControllerAnimated(true)
}
I have also tried self.navigationController?.showViewController(contractsView, sender: storyboard) but this create another view in the stack, not the original, if I can explained like this.
@IBAction func buttonSavePressed(sender: UIBarButtonItem) {
let contractsView = self.storyboard?.instantiateViewControllerWithIdentifier("ContractsTableViewControllerStoryboardID") as ContractsTableViewController
self.navigationController?.showViewController(contractsView, sender: storyboard)
}
I am new with the programming, which means that I can not aways ask the right questions. So I am posting the code, which is in working stage without comments - sorry
// ContractsTableViewController.swift
import UIKit import CoreData
class ContractsTableViewController: UITableViewController {
//, UITableViewDelegate, UITableViewDataSource
var totalContracts = 0
@IBOutlet var tableContracts: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
var appDel = (UIApplication.sharedApplication().delegate as AppDelegate)
var context = appDel.managedObjectContext!
var request = NSFetchRequest(entityName: "Contract")
request.returnsObjectsAsFaults = false
totalContracts = context.countForFetchRequest(request, error: nil)
println(totalContracts)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return totalContracts
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Default")
var appDel = (UIApplication.sharedApplication().delegate as AppDelegate)
var context = appDel.managedObjectContext!
var request = NSFetchRequest(entityName: "Contract")
request.returnsObjectsAsFaults = false
var results: NSArray = context.executeFetchRequest(request, error: nil)!
var thisContract = results[indexPath.row] as Contract
cell.detailTextLabel!.text = "From" + " " + thisContract.stratdate + " " + "to" + " " + thisContract.enddate
cell.textLabel!.text = thisContract.workingdays + " " + "days" + " " + "as" + " " + thisContract.position + " " + "on" + " " + thisContract.ship
return cell
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Default")
var appDel = (UIApplication.sharedApplication().delegate as AppDelegate)
var context = appDel.managedObjectContext!
var request = NSFetchRequest(entityName: "Contract")
request.returnsObjectsAsFaults = false
var results: NSArray = context.executeFetchRequest(request, error: nil)!
context.deleteObject(results[indexPath.row] as NSManagedObject)
context.save(nil)
totalContracts = totalContracts - 1
tableContracts.reloadData()
}
}
When your tableView controller loads back viewDidAppear
function will called. so you can reload tableView into that function like this:
override func viewDidAppear(animated: Bool) {
tableView.reloadData()
}
You can navigate to RootView this way:
self.navigationController?.popToRootViewControllerAnimated(true)
This is working fine.
HERE is your working project.