2017-04-19 13 views
0

電卓を作成しましたが、履歴をtableviewcontrollerに表示するための別の機能を追加しようとしています。私は私の計算機に入力された計算の履歴を保持するhistoryArrayを持っています。私はまた、そのデータを私のtableviewcontrollerのhistoryArray2という2番目の配列に渡すsegueを実行する機能を持っています。それは私のhistoryArray2にデータを渡すためにすべてが働いているようだ、私はブレークポイントを設定し、そこにデータを見ることができます。私の質問は、私はどのように私のtableviewcontrollerにそのデータを表示させるのですか?今私は私の電卓を実行し、tableviewcontrollerに切り替えると、それは空です。私は何を逃したのですか?ここに私のコードです:配列からtableviewcontrollerにデータを表示する方法 - swift

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. 
    } 
    */ 

} 

すべてのヘルプははるかに高く評価されるだろうが、ありがとうございました!

+0

'TableTableViewController'クラスの' numberOfRowsInSection'では、 'numberOfSections'メソッドがコメント化されています。 '/ *'と '* /'文字を削除してください。 –

+0

私はそれをして、まだ私のTableViewControllerに何も表示されません。 – walkerofskies1

+0

numberOfSections、numberOfRowsInSection、numberOfSectionsメソッドがブレークポイントを配置して呼び出されているかどうかを確認します。 –

答えて

0

さて、私はそれを得ました。プロトタイプのセル識別子「History」の名前を忘れてしまった。ここで私の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 

} 

ありがとうございました!

関連する問題