SplitView - reload data in master tableView based on detail changed in swift

左心房为你撑大大i 提交于 2019-12-10 11:04:40

问题


I create splitView with tableView in master View and tableView with static cells in two details view (see on picture). App Structure

Names of view controllers are: DocsTableViewController - master view (on the left) DocDetailTableViewController - detail view (on the top right) DocEditTableViewControler - second detail view (on the bottom right)

DocsTVC is list of doctors, DocDetailTVC is detail of doctor user selected in DocsTVC and on DocEditTVC user can edit data of doctor showed in DocDetailTVC or add completely new one (based user clicked on edit or add button).

All these things are working fine - show detail, show edit form and save edited or new doc. Problem is the master table view. I'm trying to reload it in detail after saving edited/new item and by print I see that data are reload but not the table. Table is showing still the same and I have to go to same other screen of app and then go back to see reloaded table view. What should I do?

After saving doctor on DocEditTVC I'm going back to detail to this method:

    @IBAction func saveToDocViewController (segue: UIStoryboardSegue){
    let controller = DocsTableViewController()
    controller.tableView.reloadData()
    print("[SPLIT VIEW] detail: \(controller.docs.count)")
}

And some codes from DocsTableViewControler (the master view):

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.delegate = self
    self.tableView.dataSource = self

    // menu
    if inputsMenuButton != nil {
        inputsMenuButton.target=self.revealViewController()
        inputsMenuButton.action=#selector(SWRevealViewController.revealToggle(_:))
        self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    }

    // navigation bar
    self.navigationController?.navigationBar.barTintColor = OAKScolor().colorOAKSmain()
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]

    // data load
    print("[SPLIT VIEW]: ViewDidLoad")
    dataLoad()
    self.tableView.reloadData()


    // split view
    self.splitViewController?.delegate = self
    self.splitViewController?.preferredDisplayMode = UISplitViewControllerDisplayMode.AllVisible
}

and

    func dataLoad() {
    print("[SPLIT VIEW]: DataLoad")
    let arrayOfDocIDs = PlistManager.sharedInstance.getArrayOfValuesFromDict("DocID") as [AnyObject] as! [String]

    for id in arrayOfDocIDs {
        docs.append(DocData(docID: id).docLoad(false))
    }
    print("[SPLIT VIEW] table: \(docs.count)")
}

By these prints I see that evening is going well - view did load on the master view and array of docs write new count. But the tableView is not changed. Thank you for any help.


回答1:


With this code

let controller = DocsTableViewController()
controller.tableView.reloadData()

You created new Master controller instance, not the current one. There is 2 ways to solve it:

  1. In Detail Controller, create a reference to current master controller.

  2. Use Delegate. Here is a good explanation: https://www.natashatherobot.com/explaining-ios-delegates-a-story-about-your-morning-coffee/#

Hope this help!



来源:https://stackoverflow.com/questions/39555223/splitview-reload-data-in-master-tableview-based-on-detail-changed-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!