title: how to display data from array into tableviewcontroller - swift
body: <p>I have created a calculator and am trying to add another functionality to display my history into a tableviewcontroller. I have a historyArray to hold a history of my calculations entered into my calculator. I also have a perform segue function to pass that data into a second array in my tableviewcontroller called historyArray2. It seems everything is working to pass the data to my historyArray2, I have set breakpoints and can see the data in there. My question is how do I make that data display in my tableviewcontroller? Right now when I run my calculator and switch over to the tableviewcontroller it is empty. What have I missed? here is my code:</p>
  
  <p>Viewcontroller.swift</p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController {
  
      @IBOutlet weak var displayLabel: UILabel!
      @IBOutlet weak var HistoryLabel: UILabel!
  
      var historyArray: [String] = []
      var userIsTypingNumbers = false
      var firstNumber = 0
      var secondNumber = 0
      var operation = ""
      var result = 0.0
  
      @IBAction private func NumbersEntered(_ sender: UIButton) {
  
          //know what number is being pressed
          let number = sender.currentTitle
          //if user is typing number, do this.
          if userIsTypingNumbers {
              //specify what number is being pressed.
              //append the number onto the previous number.
              displayLabel.text = displayLabel.text! + number!
          } else {
              displayLabel.text = number
              userIsTypingNumbers = true
          }
      }
      var displayValue: Double {
  
          get {
              return Double(displayLabel.text!)!
          }
          set {
              displayLabel.text = String(newValue)
          }
      }
  
      private var calculations = PerformCalculations()
  
  
      @IBAction func OperationsPressed(_ sender: UIButton) {
          userIsTypingNumbers = false
          firstNumber = Int(Double(displayLabel.text!)!)
          operation = sender.currentTitle!
          if operation == "√" {
              result = (calculations.squareroot(a: Double(firstNumber)))
              displayLabel.text = String(result)
          }
      }
  
  
      @IBAction func Enter(_ sender: UIButton) {
          userIsTypingNumbers = false
          secondNumber = Int(Double(displayLabel.text!)!)
  
  
          if operation == "+" {
              result = (calculations.add(a: Double(firstNumber), b: Double(secondNumber)))
          } else if operation == "÷" {
              result = (calculations.division(a: Double(firstNumber), b: Double(secondNumber)))
          } else if operation == "×" {
              result = (calculations.multiplication(a: Double(firstNumber), b: Double(secondNumber)))
          } else if operation == "-" {
              result = (calculations.subtract(a: Double(firstNumber), b: Double(secondNumber)))
          }
          displayLabel.text = String(result)
  
          historyArray.append("\(firstNumber) \(operation) \(secondNumber) = \(result)")
          userIsTypingNumbers = false
  
  
      }
  
  
      @IBAction func Clear(_ sender: UIButton) {
          //clear display to 0.
          displayLabel.text = "0"
      }
  
      @IBAction func Delete(_ sender: UIButton) {
          //deleting last typed number, if user messed up.
          let name: String = self.displayLabel.text!
          //count number of characters.
          let stringLength = name.characters.count
          let substringIndex = stringLength - 1
          displayLabel.text = (name as NSString).substring(to: substringIndex)
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
  
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
          if (segue.identifier == "History") {
              if let destinationVC = segue.destination as? TableTableViewController {
                  destinationVC.historyArray2 = self.historyArray
              }
          }
      }
  }
  </code></pre>
  
  <p>Model.swift</p>
  
  <pre><code>import Foundation
  
  
  class PerformCalculations {
  
      func add(a: Double, b: Double) -&gt; Double {
          let result = a + b
          return result
      }
      func division(a: Double, b: Double) -&gt; Double {
          let result = a / b
          return result
      }
      func subtract(a: Double, b: Double) -&gt; Double {
          let result = a - b
          return result
      }
      func multiplication(a: Double, b: Double) -&gt; Double {
          let result = a * b
          return result
      }
      func squareroot(a: Double) -&gt; Double {
          let result = sqrt(a)
          return result
      }
  }
  </code></pre>
  
  <p>TableTableViewController.swift</p>
  
  <pre><code>class TableTableViewController: UITableViewController {
  
  
      var historyArray2: [String] = []
  
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          // Uncomment the following line to preserve selection between presentations
          // self.clearsSelectionOnViewWillAppear = false
  
          // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
          // self.navigationItem.rightBarButtonItem = self.editButtonItem()
      }
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      // MARK: - Table view data source
  
      /*override func numberOfSections(in tableView: UITableView) -&gt; Int {
          // #warning Incomplete implementation, return the number of sections
          return 0
      }*/
  
      /*override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          // #warning Incomplete implementation, return the number of rows
          return historyArray2.count
      }
  
  
      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
  
          let cell = tableView.dequeueReusableCell(withIdentifier: "History", for: indexPath)
  
          cell.textLabel?.text = historyArray2[indexPath.row]
  
          return cell
      }*/
  
  
      /*
      // Override to support conditional editing of the table view.
      override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -&gt; Bool {
          // Return false if you do not want the specified item to be editable.
          return true
      }
      */
  
      /*
      // Override to support editing the table view.
      override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
          if editingStyle == .delete {
              // Delete the row from the data source
              tableView.deleteRows(at: [indexPath], with: .fade)
          } else if editingStyle == .insert {
              // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
          }    
      }
      */
  
      /*
      // Override to support rearranging the table view.
      override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
  
      }
      */
  
      /*
      // Override to support conditional rearranging of the table view.
      override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -&gt; Bool {
          // Return false if you do not want the item to be re-orderable.
          return true
      }
      */
  
      /*
      // MARK: - Navigation
  
      // In a storyboard-based application, you will often want to do a little preparation before navigation
      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
          // Get the new view controller using segue.destinationViewController.
          // Pass the selected object to the new view controller.
      }
      */
  
  }
  </code></pre>
  
  <p>Any help would be much appreciated, Thank You!</p>