当用户向数据库中插入一个项目然后单击后退按钮返回到 tableview 时,tableview 不会显示新项目。我必须停止该应用程序,然后再次运行它才能显示新项目。我的数据库工作得很好,所以我认为这不是问题。
我确实在 ViewDidLoad() 内的 mainController 中再次重新加载数据库。我也尝试在 ViewDidLoad() 中执行 tableView.reloadData() ,但这并没有做任何事情。
class MainViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView?
let mainDelegate = UIApplication.shared.delegate as! AppDelegate
override func viewDidLoad() {
super.viewDidLoad()
// read from the database
mainDelegate.readDataFromDatabase()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mainDelegate.people.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BookTableViewCell") as! BookTableViewCell
let rowNum = indexPath.row
cell.lblTitle?.text = mainDelegate.people[rowNum].title
cell.accessoryType = .disclosureIndicator
return cell
}
class NewBookItemViewController: UIViewController, UITextFieldDelegate, UINavigationControllerDelegate {
@IBOutlet weak var titletxt : UITextField!
@IBAction func buttonSave(sender: UIButton){
// step 18b - instantiate Data object and add textfield data
let person : Data = Data.init()
person.initWithData(theRow: 0, thetitle: title)
let mainDelegate = UIApplication.shared.delegate as! AppDelegate
// step 18c - do the insert into db
let returnCode : Bool = mainDelegate.insertIntoDatabase(person: person)
}