ios - SplitView - 根据快速更改的详细信息在主 tableView 中重新加载数据

标签 ios swift uisplitviewcontroller master-detail splitview

我在主视图中创建了带有 tableView 的 splitView,在两个详细 View 中创建了带有静态单元格的 tableView(参见图片)。 App Structure

View Controller 的名称是: DocsTableViewController - 主视图(左侧) DocDetailTableViewController - 详细 View (在右上角) DocEditTableViewControler - 第二个详细 View (在右下角)

DocsTVC 是医生列表,DocDetailTVC 是在 DocsTVC 中选择的医生用户的详细信息,在 DocEditTVC 上,用户可以编辑 DocDetailTVC 中显示的医生数据或添加全新的数据(基于用户点击编辑或添加按钮)。

所有这些都运行良好 - 显示详细信息、显示编辑表单并保存编辑过的文档或新文档。问题是主 TableView 。我试图在保存已编辑/新项目后详细重新加载它,并且通过 print 我看到数据正在重新加载但不是表格。表格显示仍然相同,我必须转到应用程序的其他屏幕,然后返回以查看重新加载的表格 View 。我该怎么办?

在 DocEditTVC 上保存 doctor 之后,我将返回此方法的详细信息:

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

还有一些来自 DocsTableViewControler(主视图)的代码:

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
}

    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)")
}

通过这些 print,我看到那天晚上进展顺利 - View 确实加载到主视图上,并且文档数组写入了新的计数。但是 tableView 没有改变。感谢您的帮助。

最佳答案

用这段代码

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

您创建了新的主 Controller 实例,而不是当前实例。 有两种方法可以解决:

  1. 在 Detail Controller 中,创建对当前主 Controller 的引用。

  2. 使用委托(delegate)。这是一个很好的解释: https://www.natashatherobot.com/explaining-ios-delegates-a-story-about-your-morning-coffee/#

希望对您有所帮助!

关于ios - SplitView - 根据快速更改的详细信息在主 tableView 中重新加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39555223/

相关文章:

ios - 为什么我的订阅的 "Share Your Information"警报显示发布者名称而不是应用程序名称?

ios - didReceiveLocalNotification下慢慢打开 map app

ios - 安装 XCode 6 beta 4 后无法运行旧的 swift 项目

ios - 集合 <__NSArrayM> 在枚举时发生了变异。

ios - 将 MarqueeLabel 转换为 Swift 1.2 语法时枚举错误

ios - 是在 AppDelegate 中的 didFinishLaunchingWithOptions 之前初始化 Controller 加载

swift - 如何在 macOS 上为 CLLocationManager 请求授权

ios - UIControl 阻止了我在 iPhone 上的所有 View

ios - 登录模态视图未显示在 Split View上

ios - 如何在紧凑型设备上快速正确使用 splitViewController 中的 didSelectRowAt ?