我有一個顯示文本的UIlabel,我希望能夠按下addButton,然后在tableViewCell中顯示該文本。我怎樣才能做到這一點呢?
class ViewController。UIViewController, UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return amountLabelArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = amountLabelArray[indexPath.row] 。
return cell
}
@IBOutlet 弱 var amountLabel: UILabel!
@IBOutlet weak var tableView: UITableView!
var amountLabelArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource=self
}
@IBAction func oneButton(_ sender: Any) {
let number = 1
amountLabel.text = amountLabel.text! String(number)
}
//this will add the amount to the table view cell[/span].
@IBAction func addButton(_ sender: Any) {
amountLabelArray.append( amountLabel.text!)
print("這是陣列( amountLabelArray)")
amountLabel.text = ""。
}
}
uj5u.com熱心網友回復:
你是否嘗試過在點擊按鈕時在你的tableView上呼叫reloadData()?
@IBAction func addButton(_ sender: Any) {
amountLabelArray.append( amountLabel.text!)
print("這是陣列( amountLabelArray)")
amountLabel.text = ""
tableView.reloadData()
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/311698.html
標籤:
