gpt4 book ai didi

uitableview - popToRootViewControllerAnimated 没有更新 UITableView

转载 作者:行者123 更新时间:2023-11-28 05:27:58 26 4
gpt4 key购买 nike

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

enter image description here

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

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

    @IBAction func buttonSavePressed(sender: UIBarButtonItem) { 

self.navigationController?.popToRootViewControllerAnimated(true)

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

    @IBAction func buttonSavePressed(sender: UIBarButtonItem) {

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

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

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

//ContractsTableViewController.swift

导入 UIKit导入核心数据

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

}

最佳答案

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

override func viewDidAppear(animated: Bool) {

tableView.reloadData()
}

您可以通过这种方式导航到 RootView:

self.navigationController?.popToRootViewControllerAnimated(true)

这工作正常。

HERE是您的工作项目。

关于uitableview - popToRootViewControllerAnimated 没有更新 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30206138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com