我有一个tableView,我在viewDidLoad()
中填充了Realm
var data = RealmModel.shared.getSections()
RealmModel.shared.fillTableview(company: "Airbus", plane: "A350", status: false)
RealmModel.shared.fillTableview(company: "Airbus", plane: "A380", status: false)
RealmModel.shared.fillTableview(company: "Boeing", plane: "Boeing 737", status: false)
data = RealmModel.shared.getSections()
statisticsTableView.reloadData()
我想用alertController
向tableView
中添加元素。
@IBAction func addPlane(_ sender: Any) {
let alertController = UIAlertController(title: "На каком самолёте вы летали?", message: nil, preferredStyle: .alert)
alertController.addTextField { (textfield) in }
let alertCancel = UIAlertAction(title: "Отменить", style: .destructive) { (alert) in }
let alertAction = UIAlertAction(title: "Добавить", style: .cancel) { (alert) in
let newItem = alertController.textFields?.first?.text
RealmModel.shared.fillTableview(company: "Другие", plane: newItem ?? "", status: true)
}
alertController.addAction(alertAction)
alertController.addAction(alertCancel)
present(alertController, animated: true, completion: nil)
statisticsTableView.reloadData()
}
在alertController
关闭后我应该如何处理tableView更新,因为现在它只在我关闭应用程序然后重新打开后更新。
发现问题,我不得不重新加载tableView内的AlertAction,而不是在
@IBAction func addPlane(_ sender: Any) {
let alertController = UIAlertController(title: "На каком самолёте вы летали?", message: nil, preferredStyle: .alert)
alertController.addTextField { (textfield) in
}
let alertCancel = UIAlertAction(title: "Отменить", style: .destructive) { (alert) in }
let alertAction = UIAlertAction(title: "Добавить", style: .cancel) { (alert) in
let newItem = alertController.textFields?.first?.text
RealmModel.shared.fillTableview(company: "Другие", plane: newItem ?? "", status: true)
self.data = RealmModel.shared.getSections()
self.statisticsTableView.reloadData()
}
alertController.addAction(alertAction)
alertController.addAction(alertCancel)
present(alertController, animated: true, completion: nil)
}