0

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:

Viewcontroller.swift

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
            }
        }
    }
}

Model.swift

import Foundation


class PerformCalculations {

    func add(a: Double, b: Double) -> Double {
        let result = a + b
        return result
    }
    func division(a: Double, b: Double) -> Double {
        let result = a / b
        return result
    }
    func subtract(a: Double, b: Double) -> Double {
        let result = a - b
        return result
    }
    func multiplication(a: Double, b: Double) -> Double {
        let result = a * b
        return result
    }
    func squareroot(a: Double) -> Double {
        let result = sqrt(a)
        return result
    }
}

TableTableViewController.swift

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) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }*/

    /*override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return historyArray2.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> 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) -> 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) -> 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.
    }
    */

}

Any help would be much appreciated, Thank You!

4
  • In TableTableViewController class numberOfRowsInSection, numberOfSections methods are commented. Remove /* and */ characters. Commented Apr 19, 2017 at 18:09
  • I did that and still not displaying anything in my TableViewController. Commented Apr 19, 2017 at 18:26
  • Check whether numberOfSections, numberOfRowsInSection, numberOfSections methods are called by placing break points. Commented Apr 19, 2017 at 18:30
  • I place a break point on numberOfSections and it seems to be getting called. When i place a break point on numberOfRowsInSection section: it's not getting to that method. Commented Apr 19, 2017 at 18:53

1 Answer 1

1

Well, I got it. I forgot to name my prototype cell identifier "History". Here is the code i needed to put into my TableTableViewController.

override fun tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) ->Int {
return historyArray.count
}


override func tableView(_tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "History", for: indexPath)

cell.textLabel?.text = historyArray2[indexPath.row]

return cell

}

Thanks for everyone's help!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.