我的应用程序中有一个名为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
}
}
感谢您的期待!
答案 0 :(得分:2)
viewDidLoad
,所以基本上它只会在视图控制器的整个生命周期中被调用一次。
一个快速的解决方案是将tableView.reloadData()
放入PastSessionsViewController viewWillAppear() or viewDidAppear()
但是,我不喜欢这种快速解决方案,因为每次解除JournalViewController
时,表格视图都会重新加载,即使用户没有更改JournalViewController
上的任何内容(例如,取消编辑)。因此,我建议在PastSessionsViewController
和JournalViewController
之间使用委托方法,当用户实际编辑JournalViewController
上的数据,然后通知PastSessionsViewController
刷新表格。
答案 1 :(得分:1)
您目前仅在initSessionsDataSource的init上准备条目,但在您更改CoreData之后不会。因此,每次为tableView重新加载Data时,都会使用最初加载的相同数据集。作为快速入侵,您可以尝试以下列方式更新viewDidAppear:
{{1}}
答案 2 :(得分:1)
根据您列出的代码,tableView
内的nil
媒体资源可能为viewDidAppear
。原因是在viewDidLoad
中您将UITableView
构造为tableView
,这是一个局部变量。您需要将该变量分配给属性:
self.tableView = tableView