我希望tableview插入一个新行,其中的文本对应于我按下的特定按钮,但我不确定如何在我的代码/ UI中指定它。
基本上,当我按下按钮#1,“按下按钮#2”按下按钮#2按下按钮时,我希望显示“按下按钮#1”按钮,如何按下按钮#2等。如何配置?< / p>
TableViewController.swift
import UIKit
class TableViewController: UITableViewController {
var myArray = ["button #1 pressed", "button #2 pressed", "button #3 pressed"]
override func viewDidLoad() {
super.viewDidLoad(
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = self.FruitVegArray[indexPath.row]
return cell
}
}
SubViewController.swift
import UIKit
class SubViewController: UIViewController {
@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let tableVC = segue.destination as! TableViewController
}
@IBAction func button1action(_ sender: Any) {
}
@IBAction func button2action(_ sender: Any) {
}
}
答案 0 :(得分:1)
将myArray初始化为空白
var myArray = [String]()
并从myArray中返回行数。
override func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return myArray.count
}
现在按下button1然后只需按下按钮#1&#34;数组和刷新表视图中的文本。您需要从&#34; SubViewController&#34;访问myArray。然后添加文本。
myArray.append("button #1 pressed")
tableView.beginUpdates()
tableView.insertRows(at: [IndexPath(row: myArray.count-1, section: 0)], with: .automatic)
tableView.endUpdates()
按钮2或其他按钮相同。