Swift - 在解除视图控制器后不重新加载表视图数据

时间:2015-06-28 23:50:21

标签: ios swift uitableview core-data

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

当用户编辑一个条目并将其保存(保存到CoreData)时,请忽略JournalViewController我希望PastSessions中的表视图反映这些更改并显示更新的表格单元格。< / p>

我在tableView.reloadData() PastSessionsViewController中呼叫viewDidLoad(),但这似乎不起作用。我还添加了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
        }
    }

感谢您的期待!

3 个答案:

答案 0 :(得分:2)

当视图控制器第一次加载视图时,会调用

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

答案 1 :(得分:1)

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

{{1}}

答案 2 :(得分:1)

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

self.tableView = tableView