【问题标题】:Swift · Dismissing Modal ViewController and reload tableview afterwardsSwift · Dismissing Modal ViewController and reload tableview after
【发布时间】:2020-07-23 12:11:09
【问题描述】:

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

我尝试了其他有关堆栈溢出的解决方案,但不知何故,我的应用程序在按下 Load New Film 按钮后总是崩溃。我认识到,大多数用户在代码中调用 Modal ViewController,这对我也不起作用。

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

您能帮我重新加载 tableView 吗?

这是我的代码:

这是主视图控制器:

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


    【解决方案1】:

    当您关闭模式时,您可以在 ccompletion 中添加代码,粘贴 loadFilmRolls 函数,它应该会刷新它。

      override func dismiss(animated flag: Bool,
                             completion: (() -> Void)?) {
        super.dismiss(animated: flag, completion: completion)
        loadFilmRolls()
    }
    

    我没有测试过它,但它应该对你有用。

    【讨论】:

      【解决方案2】:

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

      dismiss(animated: true, completion: {
          if let viewController = presentingViewController as? LoadedFilmsTableViewController {
              viewController.tableView.reloadData()
          }
      })
      

      【讨论】:

      • 感谢您的快速响应。不幸的是,我收到以下错误:? 在闭包中隐式使用“self”;使用“自我”。为了明确捕获语义我尝试通过添加 self:dismiss(animated: true, completion: { if let viewController = self.presentingViewController as? LoadedFilmsTableViewController { viewController.tableView.reloadData() } }) 来修复它,结果在关闭视图后没有任何反应——仍然没有重新加载。有什么想法吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      相关资源
      最近更新 更多