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

问题是,在按下“保存”按钮后,我想不出如何重新加载表中的数据。
我尝试在@IBAction (发送方: UIBarButtonItem)中使用buttonSavePressed(真),但这只是将我发送到TableView,而不重新加载数据。
@IBAction func buttonSavePressed(sender: UIBarButtonItem) {
self.navigationController?.popToRootViewControllerAnimated(true)}
我也尝试过self.navigationController?.showViewController(contractsView,发送者:故事板),但是这会在堆栈中创建另一个视图,而不是原始视图,如果我可以这样解释的话。
@IBAction func buttonSavePressed(sender: UIBarButtonItem) {
let contractsView = self.storyboard?.instantiateViewControllerWithIdentifier("ContractsTableViewControllerStoryboardID") as ContractsTableViewController
self.navigationController?.showViewController(contractsView, sender: storyboard) }
我是新编程的,这意味着我不能总是问正确的问题。因此,我在张贴代码,这是在工作阶段,没有评论-对不起
// ContractsTableViewController.swift
导入UIKit导入CoreData
类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()
}}
发布于 2015-05-13 06:01:13
当您的tableView控制器加载回时,viewDidAppear函数将被调用。所以您可以像这样将tableView重新加载到该函数中:
override func viewDidAppear(animated: Bool) {
tableView.reloadData()
}您可以这样导航到RootView:
self.navigationController?.popToRootViewControllerAnimated(true)这很好用。
这里是您的工作项目。
发布于 2015-05-13 05:16:00
首先,以IBOutlet of UITableView in ContractsViewController为例。然后使用以下代码重新加载表视图:
self.myTableView.reloadData()https://stackoverflow.com/questions/30206138
复制相似问题