简体   繁体   中英

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

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 print s 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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM