我正在尝试创建一个简单的应用程序,它允许将数据追加到第一个ViewController中的数组中,然后通过segue将其发送到第二个ViewController。但是,它不是追加新数据,而是更新数组的第一个索引。
import UIKit
class ViewController: UIViewController {
var data = [String]()
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func sendButton(_ sender: Any) {
if textField.text != nil {
let activity = textField.text
data.append(activity!) //Data should be appended here.
performSegue(withIdentifier: "sendSegue", sender: self)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "sendSegue" {
let destVC: AppTableViewController = segue.destination as! AppTableViewController
destVC.newData = data
}
}
}
这是我的第二个tableviewcontroller,它只是更新第一个索引,所以我只是不断更改第一个索引,而不是在其中添加新值。
import UIKit
class AppTableViewController: UITableViewController {
var newData = [String]()
@IBOutlet var myTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return newData.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = newData[indexPath.row]
return cell
}
}
最佳答案
基于这个link,
请将函数名sendButton
更改为btnSendButton
。
以便它在prepare
之前运行。
目前sendButton
正在prepare
之后运行。所以数据没有追加。
关于ios - TableView不追加新数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47005366/