【问题标题】:Xcode swift: My unwind segue doesn't add to my tableViewXcode swift:我的 unwind segue 没有添加到我的 tableView
【发布时间】:2016-08-31 22:13:02
【问题描述】:

我一直在松散地遵循“Meal Tracker”应用程序的 Apple 开发人员教程(在此处找到:https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson8.html#//apple_ref/doc/uid/TP40015214-CH16-SW1),虽然我之前完整地构建了该应用程序,并且它工作正常,但我正在尝试制作一个修改版本,我遇到了障碍。一切正常,直到我尝试实现“保存按钮”以将新项目添加到使用展开 segue 的表视图中。 segue 工作得很好,我想传递的所有信息都会适当地打印到控制台,但由于某种原因,我的新单元格不会添加到表格视图中。

这是“准备转场”的代码:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if saveButton === sender {
        let name = nameTextField.text ?? ""
        let date = dateLabel.text ?? ""
        let price = priceTextField.text ?? ""
        let balance = 100.00

        // Set the purchase to be passed to PurchaseTableViewController after the unwind segue.
        purchase = Purchase(name: name, date: date, price: price, balance: balance)
    }
}

这里是“展开购买清单”的代码:

@IBAction func unwindToPurchaseList(sender: UIStoryboardSegue) {
    if let sourceViewController = sender.sourceViewController as? AddPurchaseViewController, purchase = sourceViewController.purchase {
        // Add a new purchase.
        let newIndexPath = NSIndexPath(forRow: purchases.count, inSection: 0)
        purchases.append(purchase)
        tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom)
    }
}

【问题讨论】:

    标签: xcode swift uitableview tableview


    【解决方案1】:

    尝试 tableview 开始更新和结束更新

    @IBAction func unwindToPurchaseList(sender: UIStoryboardSegue) {
    if let sourceViewController = sender.sourceViewController as? AddPurchaseViewController, purchase = sourceViewController.purchase {
        // Add a new purchase.
        let newIndexPath = NSIndexPath(forRow: purchases.count, inSection: 0)
        tableView.beginUpdates()
        purchases.append(purchase)
        tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom)
        tableView.endUpdates()
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-07-24
      • 1970-01-01
      • 1970-01-01
      • 2013-04-30
      • 2016-02-29
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多