在标签中显示一个包含 10 个 Int 的数组

Show an Array of 10 Ints in a label

我正在尝试构建一个基本的乘法应用程序,它将根据您在文本字段中键入的数字显示您的 1 倍表最多 10 倍表。

当我 运行 我的代码时,我的标签只产生 1 行,这是最后一次乘法计算的结果。例如 10 乘以 8 是 80,其他 9 不被添加,在此之后我的完整数组显示显示表格的正确答案。这是我的代码

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var numberTextField: UITextField!

    @IBOutlet weak var tablesLabel: UILabel!

    var tablesArray = [Int]()

    var number = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    @IBAction func tablesButtonTapped(_ sender: AnyObject) {

        if numberTextField.text == "" {
            print("Sorry you must enter a number between 1 and 10")
        } else {
            tablesArray = []
            tablesLabel.text = ""
            multiplyBy(number: Int(numberTextField.text!)!)
            numberTextField.text = ""
        }
    }
    func multiplyBy(number: Int) {

        for index in 1...10 {
           tablesArray.append(index * number)
            print(tablesArray)
            tablesLabel.text = ("\(index) times \(number) is \(tablesArray)")
        }
        tablesLabel.isHidden = false
    }
}

改变

tablesLabel.text = ("\(index) times \(number) is \(tablesArray)")

tablesLabel.text = ("\(tablesLabel.text ?? "") \(index) times \(number) is \(tablesArray)")

您每次都覆盖了标签的文本。这不会覆盖标签的文本并向其添加新文本。