繁体   English   中英

Swift-关闭视图控制器后不重新加载表视图数据

[英]Swift – Table view data not reloading after dismissing view controller

我在名为JournalViewController应用程序中有一个视图,该视图通过PastSessionsViewController呈现。 PastSessions具有一个表格视图,用户可以点击该表格视图来编辑和调出日记。

当用户编辑的条目并保存(保存到CoreData),驳回JournalViewController我想在表视图PastSessions ,以反映这些变化,并显示更新的表格单元格。

我在PastSessionsViewController viewDidLoad()调用tableView.reloadData()PastSessionsViewController似乎不起作用。 我还添加了一个委托JournalViewController与互动PastSessionsViewController提前dismissViewController

以下是一些代码:

PastSessionsViewController

class PastSessionsViewController: UIViewController, UITableViewDelegate, JournalVCDelegate {
    weak var tableView: UITableView?
    weak var backButton: UIButton?

    let pastSessionsDataSource: PastSessionsDataSource

    init() {
        pastSessionsDataSource = PastSessionsDataSource()
        super.init(nibName: nil, bundle: nil)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        let tableView = UITableView()
        tableView.backgroundColor = nil
        tableView.delegate = self
        tableView.dataSource = pastSessionsDataSource
        tableView.registerClass(EntryCell.self, forCellReuseIdentifier: "cell")
        view.addSubview(tableView)
        self.tableView = tableView
    }

    override func viewDidAppear(animated: Bool) {
        tableView?.reloadData()
    }

    func didFinishJournalVC(controller: JournalViewController) {
        var newDataSource = PastSessionsDataSource()
        tableView?.dataSource = newDataSource
        // tried this ^, but it's causing the app to crash 

        // tableView?.reloadData() <- this isn't doing the trick either
        dismissViewControllerAnimated(true, completion: nil)
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let editJournalVC = JournalViewController(label: "Edit your thoughts")
        editJournalVC.delegate = self

        presentViewController(editJournalVC, animated: true, completion: nil)
    }
}

JournalViewController

protocol JournalVCDelegate {
    func didFinishJournalVC(controller: JournalViewController)
}

class JournalViewController: UIViewController, UITextViewDelegate {
    var delegate: JournalVCDelegate! = nil

    func doneJournalEntry(sender: UIButton) {
        journalEntryTextArea?.resignFirstResponder()

        ... do some core data saving ...
        delegate.didFinishJournalVC(self)
    }
}

PastSessionsDataSource

   import UIKit
   import CoreData

   class PastSessionsDataSource: NSObject {
        var arrayOfEntries = [Entry]()
        var coreDataReturn: [Meditation]?

        func prepareEntries() {
          // gets stuff from coredata and formats it appropriately
        }

        override init() {
            super.init()
            prepareEntries()
        }
    }

    extension PastSessionsDataSource: UITableViewDataSource {
        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return arrayOfEntries.count
        }

        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! EntryCell

            ... set up the labels in the cell ...

            return cell
        }
    }

感谢您的光临!

viewDidLoad在视图控制器第一次加载其视图时被调用,因此,基本上,它将仅在视图控制器的整个生命周期中被调用一次。
一种快速的解决方案是将tableView.reloadData()放在PastSessionsViewController viewWillAppear() or viewDidAppear()
但是,我不喜欢这种快速解决方案,因为每次您关闭JournalViewController ,即使用户没有在JournalViewController进行任何更改(例如,取消编辑),表视图也会重新加载。 因此,我建议在用户实际在JournalViewController上编辑数据然后通知PastSessionsViewController刷新表时,在PastSessionsViewControllerJournalViewController之间使用委托方法。

当前,您仅在PastSessionsDataSource的init上准备条目,但在更改CoreData之后不准备。 因此,每次为tableView重新加载data时,都使用最初加载的相同数据集。 作为快速攻克,您可以尝试通过以下方式更新viewDidAppear:

 override func viewDidAppear(animated: Bool) {
        if let tableView = tableView {
          let dataSource = tableView.dataSource! as PastSessionsDataSource
          dataSource.prepareEntries()
          tableView.reloadData()
        }
 }

根据列出的代码,您的tableView属性在viewDidAppear可能为nil 原因是在viewDidLoad您将UITableView构造为tableView ,这是一个局部变量。 您需要将该变量分配给属性:

self.tableView = tableView

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM