按下按钮时如何在表视图单元格中显示标签文本?



我有这个显示文本的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 weak 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
@IBAction func addButton(_ sender: Any) {

amountLabelArray.append(amountLabel.text!)
print("This is the array (amountLabelArray)")
amountLabel.text = ""
}
}

您是否尝试在tableView上调用reloadData()按钮点击?

@IBAction func addButton(_ sender: Any) {
amountLabelArray.append(amountLabel.text!)
print("This is the array (amountLabelArray)")
amountLabel.text = ""
tableView.reloadData()
}

相关内容

最新更新