2016-08-18 2 views
0

텍스트 필드에 입력하는 숫자에 따라 1 배 테이블을 최대 10 배 테이블로 표시하는 기본 곱셈 앱을 작성하려고합니다.레이블에 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 
    } 
} 
+0

마지막까지 루프가 반복되므로 배열에 저장해야합니다. –

답변

0

변경

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

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

하는 것입니다 당신은 레이블의 텍스트 때마다 덮어 씁니다. 라벨의 텍스트를 덮어 쓰지 않고 새 텍스트를 추가합니다.