首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >popToRootViewControllerAnimated没有更新UITableView

popToRootViewControllerAnimated没有更新UITableView
EN

Stack Overflow用户
提问于 2015-05-13 05:08:20
回答 2查看 1.4K关注 0票数 1

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

问题是,在按下“保存”按钮后,我想不出如何重新加载表中的数据。

我尝试在@IBAction (发送方: UIBarButtonItem)中使用buttonSavePressed(真),但这只是将我发送到TableView,而不重新加载数据。

代码语言:javascript
运行
复制
    @IBAction func buttonSavePressed(sender: UIBarButtonItem) { 

    self.navigationController?.popToRootViewControllerAnimated(true)

}

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

代码语言:javascript
运行
复制
    @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 {

代码语言:javascript
运行
复制
//, 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()

}

}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-13 06:01:13

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

代码语言:javascript
运行
复制
override func viewDidAppear(animated: Bool) {

    tableView.reloadData()
}

您可以这样导航到RootView:

代码语言:javascript
运行
复制
self.navigationController?.popToRootViewControllerAnimated(true)

这很好用。

这里是您的工作项目。

票数 2
EN

Stack Overflow用户

发布于 2015-05-13 05:16:00

首先,以IBOutlet of UITableView in ContractsViewController为例。然后使用以下代码重新加载表视图:

代码语言:javascript
运行
复制
self.myTableView.reloadData()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30206138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档