M.J. M.J. - 3 months ago 9
Swift Question

Can't get new items to save to tableview

Ugh please help.... I'm new to Swift (coding in general) however I used this same code to make an earlier app that had a VC with 1 text field, a rating control and an image and worked fine. This version has 2 text fields and a date picker but only the sample data appears in the tableView when trying to add an new item.

enter image description here
So here after clicking the + button you're taken to the next page where you can add new information (below) upon pressing save the new information should appear in the starting tableview.

enter image description here

import UIKit

class BudgetTableViewController: UITableViewController {

var budgets = [Budget]()

override func viewDidLoad() {


func loadSampleBudgets() {

let budget1 = Budget(date: "8/16/2016", name: "Eyebrows", amount: "15")!
let budget2 = Budget(date: "8/28/2016", name: "Acme", amount: "59")!
let budget3 = Budget(date: "9/10/2016", name: "Wildwood", amount: "199")!

budgets += [budget1, budget2, budget3]
override func didReceiveMemoryWarning() {
// Dispose of any resources that can be recreated.

// Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return budgets.count

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "BudgetTableViewCell"

let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! BudgetTableViewCell
let budget = budgets[indexPath.row]

cell.dateLabel.text = budget.date
cell.nameLabel.text = budget.name
cell.amountLabel.text = budget.amount
return cell

@IBAction func unwindToMealList(sender: UIStoryboardSegue) {
if let sourceViewController = sender.sourceViewController as? BudgetViewController, budget = sourceViewController.budget {
//Add a new meal
let newIndexPath = NSIndexPath(forRow: budgets.count, inSection: 0)

break second VC

import UIKit

class BudgetViewController: UIViewController, UITextFieldDelegate, UINavigationControllerDelegate {

// Properties:
@IBOutlet weak var datePicker: UIDatePicker!
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var amountTextField: UITextField!
@IBOutlet weak var dateDisplay: UILabel!
@IBOutlet weak var saveButton: UIBarButtonItem!

var budget: Budget?

// Date picker:
let dateFormatter = NSDateFormatter()

func setDate() {
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
dateDisplay.text = dateFormatter.stringFromDate(datePicker.date)

// Navigation

// This method lets you configure a view controller before it's presented
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if saveButton === sender {

let name = nameTextField.text ?? ""
let date = dateDisplay.text ?? ""
let amount = amountTextField.text ?? ""

// set the budget to be passed to the Controller, this code configures the meal prperty with the appropriate values before the segue executes

budget = Budget(date: date, name: name, amount: amount)


// Actions:
@IBAction func datePickerChanger(sender: AnyObject) {

override func viewDidLoad() {

// Handle the text field

nameTextField.delegate = self
amountTextField.delegate = self
// UITextFieldDelegate

func textFieldShouldReturn(textField: UITextField) -> Bool {
return true
func textFieldDidEndEditing(textField: UITextField) {




You should call insertRowsAtIndexPaths:withRowAnimation or reloadData method for update table view after adding new data to budgets(table view's data source)