![](/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.