我正在編輯第二個viewController上的電子郵件。如果我修改第二個視圖(tableViewBudget)中的電子郵件地址,但表格視圖Cell不更改,Label.text(在ViewController中)確實會更改。它始終顯示savedEmail.text的第一個值。我如何修改我的代碼,以便tableView單元格在我更改另一個表格視圖中的字符串時發生變化?謝謝!ios Reloading TableView
import UIKit
class ViewController:UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var savedEmail: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func exitToHere (sender: UIStoryboardSegue){
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
cell.textLabel?.text = savedEmail.text
return cell
}
}
第二個視圖控制器
import UIKit
class TableviewBudget: UIViewController {
@IBOutlet weak var emailField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func updateEmail(_ sender: Any) {
(presentingViewController as! ViewController).savedEmail.text =
self.emailField.text
}
@IBAction func hideKeyboard(_ sender: Any) {
self.emailField.resignFirstResponder()
}
override func viewWillAppear(_ animated: Bool) {
self.emailField.text = (presentingViewController as! ViewController).savedEmail.text
super.viewWillAppear(animated)
}
}
嘗試在updateEmail方法內的表上調用reloadData()? – nodyor90z