![](/img/trans.png)
[英]Problem reloading gridView data after dismissing a modal view controller
[英]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
刷新表时,在PastSessionsViewController
和JournalViewController
之间使用委托方法。
当前,您仅在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.