按下按钮时如何在 table 视图单元格中显示标签文本?
How do I display label text in table view cell when pressing a button?
我有一个显示文本的 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()
}
我有一个显示文本的 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()
}