popToRootViewControllerAnimated 没有更新 UITableView

popToRootViewControllerAnimated is not updating UITableView

我正在开发一个带有嵌入式导航控制器和 UIViewController 的 UITableView 的小型 CoreData 程序,在其中添加 CoreData。

问题是我不知道如何在按下保存按钮后重新加载 table 中的数据。

我尝试在@IBAction func buttonSavePressed(sender: UIBarButtonItem) 中使用 popToRootViewControllerAnimated(true),但这只是将我发送到 TableView,而没有重新加载数据。

    @IBAction func buttonSavePressed(sender: UIBarButtonItem) { 

    self.navigationController?.popToRootViewControllerAnimated(true)

}

我也尝试过 self.navigationController?.showViewController(contractsView, sender: storyboard) 但这会在堆栈中创建另一个视图,而不是原始视图,如果我可以这样解释的话。

    @IBAction func buttonSavePressed(sender: UIBarButtonItem) {

   let contractsView = self.storyboard?.instantiateViewControllerWithIdentifier("ContractsTableViewControllerStoryboardID") as ContractsTableViewController

    self.navigationController?.showViewController(contractsView, sender: storyboard) 

}

我是编程新手,这意味着我无法提出正确的问题。所以我发布了代码,它处于工作阶段,没有评论 - 抱歉

// ContractsTableViewController.swift

导入 UIKit 导入核心数据

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()

}

}

首先拿ContractsViewController中UITableView的IBOutlet。然后使用以下代码重新加载表格视图:

self.myTableView.reloadData()

当您的 tableView 控制器加载回来时 viewDidAppear 函数将被调用。所以你可以像这样将 tableView 重新加载到该函数中:

override func viewDidAppear(animated: Bool) {

    tableView.reloadData()
}

您可以这样导航到 RootView:

self.navigationController?.popToRootViewControllerAnimated(true)

这工作正常。

HERE 是您的工作项目。