Xcode swift:我的unwind segue没有添加到我的tableView

时间:2016-05-06 00:05:10

标签: xcode swift uitableview tableview

我一直在松散地关注Apple开发者关于“Meal Tracker”应用程序的教程(在这里找到:{{3}}),虽然我之前已经完整地构建了应用程序,并且它有效,但我正在尝试制作一个修改版,我遇到了障碍。在我尝试实现“保存按钮”以将新项添加到使用展开segue的表视图之前,一切都有效。 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)
    }
}

1 个答案:

答案 0 :(得分:0)

尝试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()
}
}