首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将数组中的数据显示到tableviewcontroller - swift

如何将数组中的数据显示到tableviewcontroller - swift
EN

Stack Overflow用户
提问于 2017-04-20 00:47:06
回答 1查看 103关注 0票数 0

我已经创建了一个计算器,并且正在尝试添加另一个功能,以将我的历史记录显示到tableviewcontroller中。我有一个historyArray,用来保存我输入到计算器中的计算历史。我还有一个执行分段函数,用于将数据传递到表视图控制器中名为historyArray2的第二个数组中。似乎一切都在工作,将数据传递给我的historyArray2,我已经设置了断点,并且可以看到其中的数据。我的问题是如何让数据显示在我的tableviewcontroller中?现在,当我运行我的计算器并切换到tableviewcontroller时,它是空的。我错过了什么?下面是我的代码:

Viewcontroller.swift

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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.
    }
    */

}

任何帮助都将不胜感激,谢谢!

EN

回答 1

Stack Overflow用户

发布于 2017-04-20 04:35:27

好吧,我知道了。我忘记将我的原型单元标识符命名为"History“。下面是我需要放入TableTableViewController中的代码。

代码语言:javascript
复制
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

}

感谢大家的帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43501595

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档