如何将数据从数组显示到tableviewcontroller - swift

时间:2017-04-19 16:47:06

标签: ios

我已经创建了一个计算器,并且我正在尝试添加另一个功能来将我的历史记录显示到tableviewcontroller中。我有一个historyArray来保存我的计算历史记录。我还有一个执行segue函数将该数据传递到我的tableviewcontroller中名为historyArray2的第二个数组中。似乎一切都在努力将数据传递给我的historyArray2,我设置了断点并且可以看到那里的数据。我的问题是如何在tableviewcontroller中显示数据?现在,当我运行我的计算器并切换到tableviewcontroller时,它是空的。我错过了什么?这是我的代码:

Viewcontroller.swift

Article

Model.swift

public class Article {

    private final String mTitle;
    private final String mDescription;
    private final String mUrl;
    private final String mImageUrl;


    public Article(String title, String description, String url, String imageUrl) {
        mTitle = title;
        mDescription = description;
        mUrl = url;
        mImageUrl = imageUrl;
    }

    public String title() {
        return mTitle;
    }

    public String description() {
        return mDescription;
    }

    public String url() {
        return mUrl;
    }

    public String imageUrl() {
        return mImageUrl;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Article other = (Article) o;

        return mTitle != null && mTitle.equals(other.mTitle) &&
                mDescription != null && mDescription.equals(other.mDescription) &&
                mUrl != null && mUrl.equals(other.mUrl) &&
                mImageUrl != null && mImageUrl.equals(other.mImageUrl);
    }

    @Override
    public int hashCode() {
        int result = mTitle != null ? mTitle.hashCode() : 0;
        result = 31 * result + (mDescription != null ? mDescription.hashCode() : 0);
        result = 31 * result + (mUrl != null ? mUrl.hashCode() : 0);
        result = 31 * result + (mImageUrl != null ? mImageUrl.hashCode() : 0);
        return result;
    }

}

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

非常感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

嗯,我明白了。我忘了给我的原型细胞标识符命名"历史"。这是我需要放入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

}

感谢大家的帮助!