Swift·关闭模式ViewController并随后重新加载tableview

问题描述 投票:0回答:2

这是我的第一个iOS应用。我正在使用Modal ViewlController保存新的列表条目。不幸的是,之后我无法用新条目更新tableView。Here is a screen cast o the current state of the app.

我在堆栈溢出时尝试了其他解决方案,但是按Load New Film Button后,我的应用总是以某种方式崩溃。我认识到,大多数用户都在代码中调用Modal ViewController,这对我也不起作用。

所以我在情节提要中做到了:screencast Modal ViewController via segue

请您帮我重新加载tableView吗?

这是我的代码:

这是主要的ViewController:

import UIKit
import RealmSwift

class LoadedFilmsTableViewController: UITableViewController {

    let realm = try! Realm()
    var filmRolls: Results<FilmRoll>?


    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(UINib(nibName: K.cellNibNameLoaded, bundle: nil), forCellReuseIdentifier: K.CellIdentifier.CellFilmRoll)
        loadFilmRolls()
    }        

    //MARK: - TableView Datasource Methods
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return filmRolls?.count ?? 1
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let df = DateFormatter()
        df.dateFormat = "d. MMMM yyyy"
        let dateAsString = df.string(from: filmRolls?[indexPath.row].dateLoaded ?? Date())

        let cell = tableView.dequeueReusableCell(withIdentifier: K.CellIdentifier.CellFilmRoll, for: indexPath) as! LoadedFilmCell
        cell.labelFilmStock.text = filmRolls?[indexPath.row].filmStock ?? "No film roll added yet."
        cell.labelCamera.text = filmRolls?[indexPath.row].camera ?? "No film roll added yet."
        cell.labelDateLoaded.text = dateAsString
        return cell
    }


    //MARK: - Add new Film Rolls
    @IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
    }

    func loadFilmRolls() {
        filmRolls = realm.objects(FilmRoll.self)
        tableView.reloadData()
    }        
}

这是模式视图:

import UIKit
import RealmSwift

class AddNewRollViewController: UIViewController {

    let realm = try! Realm()
    var filmRolls: Results<FilmRoll>?


    @IBOutlet weak var textFieldFilmStock: UITextField!
    @IBOutlet weak var textFieldCamera: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        textFieldFilmStock.placeholder = "Kodak Color Plus 200"
        textFieldCamera.placeholder = "Canon AE-1"
    }

    @IBAction func addNewFilmRollButtonPressed(_ sender: UIButton) {
        let newFilmRoll = FilmRoll()
        newFilmRoll.filmStock = textFieldFilmStock.text!
        newFilmRoll.camera = textFieldCamera.text!
        newFilmRoll.dateLoaded = Date()

        do {
            try realm.write {
                realm.add(newFilmRoll)
            }
        } catch {
            print("Error saving FilmRoll \(error)")
        }

        navigationController?.popViewController(animated: true)
        dismiss(animated: true, completion: nil)
    }
}
ios swift swift5 modalviewcontroller xcode11.4
2个回答
0
投票

dismiss(animated: true, completion: nil)中,您可以提供一个完成块,将在关闭视图后执行该块


0
投票

当您关闭模态时,您是否可以在函数中添加代码,将loadFilmRolls函数插入其中,它应该刷新它。

© www.soinside.com 2019 - 2024. All rights reserved.