简体   繁体   English

iOS Swift如何从其他控制器重新加载tableView

[英]IOS Swift how can I reload a tableView from a different controller

I have 2 controllers A and Controller B . 我有2个控制器A和控制器B。 Controller A has a TableView and Controller B is a subview that when clicked opens a form and on Submit it enters data into the database. 控制器A具有一个TableView,控制器B是一个子视图,单击该子视图将打开一个表单,并在“提交”时将数据输入数据库。 My problem is that I attempt to reload my TableView from Controller B from the user hits submit and I get the following error fatal error: unexpectedly found nil while unwrapping an Optional value from this line 我的问题是,我尝试从用户命中提交中的控制器B重新加载TableView,并且收到以下错误致命错误:在从此行解开Optional值时意外发现nil

 self.TableSource.reloadData()

Now the data from Controller B is successfully inserted so after I restart my app the data I submit is there . 现在,已成功插入控制器B中的数据,因此在我重新启动应用程序之后,我提交的数据就在那里了。 This is my code (TableSource is the TableView outlet) 这是我的代码(TableSource是TableView出口)

Controller A 控制器A

  func reloadTable(latmin: Float,latmax: Float,lonmin: Float, lonmax: Float) {
        let url:URL = URL(string:ConnectionString+"MY-URL")!



        let session = URLSession.shared
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
        let parameter = "parameters"
        request.httpBody = parameter.data(using: String.Encoding.utf8)
        session.dataTask(with:request, completionHandler: {(data, response, error) in
            if error != nil {

            } else {
                do {

                    let parsed = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String:Any]



                    if let S = parsedData["myData"] as? [AnyObject]            {


                        for A in Data {
                           // gets Json Data

                                       }

                        DispatchQueue.main.async {
                    // This is what I named my TableView
                            self.TableSource.reloadData()

                        }
                    }

                } catch let error as NSError {
                    print(error)
                }

            }

        }).resume()


    }

That is my HTTP-Request that gets data from the database, now in that same Controller AI have a button that when clicked opens the SubView to Controller B and this is the code 那是我从数据库获取数据的HTTP请求,现在在同一Controller AI中有一个按钮,单击该按钮可将SubView打开到Controller B,这是代码

  @IBAction func Post_Action(_ sender: Any) {
        let Popup = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ControllerB") as! Controller B

        self.addChildViewController(Popup)
        Popup.view.frame = self.view.frame
        self.view.addSubview(Popup.view)
        Popup.didMove(toParentViewController: self)
    }

This is the code in Controller B and this is how I try to reload the TableView in Controller A 这是控制器B中的代码,这就是我尝试在控制器A中重新加载TableView的方式

@IBAction func Submit_Form(_ sender: Any) {

  // Code that submits the form no issues here


        latmin = 32.18
       latmax = 32.50
       lonmin = -81.12
       lonmax = -81.90

     let Homepage = ControllerA()
       Homepage.reloadTable(latmin: latmin!,latmax: latmax!,lonmin: lonmin!,lonmax: lonmax!)

    }

So as stated before Controller A loads the data from the Database, Controller B has a form and when submitted it enters new data into the database . 因此,如在控制器A从数据库加载数据之前所述,控制器B具有一个表单,并且在提交时将新数据输入数据库。 That whole process works I just now want to update the TableView in Controller A from the form is submitted in Controller B 整个过程正常工作,我现在想从控制器B中提交的表单更新控制器A中的TableView

You can do it with NSNotification 您可以使用NSNotification做到这一点

in swift 3.0 在迅速3.0

Think you have two viwe controllers called viewcontrollerA and viewControllerB 认为您有两个viwe控制器,分别称为viewcontrollerAviewControllerB

  • viewcontrollerA has the tableview. viewcontrollerA具有表视图。
  • you need to reload it from viewcontrolerB 您需要从viewcontrolerB重新加载

implementaion of viewcontrollerA 视图控制器的实现

create a function to relod your tableview in viewcontrollerA and call it in viewDidLoad 创建一个函数以在viewcontrollerA重载tableview并在viewDidLoad调用它

override func viewDidLoad() {
    let notificationNme = NSNotification.Name("NotificationIdf")
    NotificationCenter.default.addObserver(self, selector: #selector(YourControllername.reloadTableview), name: notificationNme, object: nil)
}

func relodaTableview() {
    self.TableSource.reloadData()
}

implementation in viewcontrollerB (where you want to reload tableview) viewcontrollerB中的实现(您要在其中重新加载tableview)

post the notification in button click or anywhere you want like below in button click or anywhere you want like below发布通知

 let notificationNme = NSNotification.Name("NotificationIdf")
 NotificationCenter.default.post(name: notificationNme, object: nil)

hope this will help to you. 希望对您有帮助。

I would suggest using protocol: 我建议使用协议:

protocol SomeActionDelegate {
    func didSomeAction()
}

In ViewController B 在ViewController B中

var delegate: SomeActionDelegate?

In ViewController A when segue 在ViewController A中时

viewControllerB.delegate = self

You should add this 你应该添加这个

extension ViewControllerA: SomeActionDelegate {
    func didSomeAction() {
         self.tableView.reloadData()
    }
}

And in ViewController B 在ViewController B中

 func didChangeSomething() {
      self.delegate?.didSomeAction()
 }

It works like when ViewController B didChangeSomething() it sends message to ViewController A that it should didSomeAction() 就像当ViewController B didChangeSomething()它将消息发送给ViewController A时,它应该执行didSomeAction()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何快速从另一个视图控制器重新加载 tableview - How to reload tableview from another view controller in swift 单击搜索栏上的“取消”时如何重新加载tableview中的所有数据?(IOS / Swift) - How can I reload all of data in tableview when I click cancel on search bar?(IOS/Swift) iOS swift tableview触摸按钮时如何重新加载数据 - iOS swift tableview how can I reload the data when I touch button 如何使用视图控制器 (swift) 重新加载 tableview - How to reload tableview using view controller (swift) 如何在Swift中从其他ViewController重新加载TableView中的数据 - How to reload data in a TableView from a different ViewController in Swift 如何在iPhone的View Controller中的TableView中重新加载数据 - How can I reload data in tableview in view controller in iphone 如何在swift 4中正确地在tableview控制器单元内的tableview中重新加载数据? - how to reload data in tableview inside tableview controller cell correctly in swift 4? 如何从DetailViewController重新加载MasterView Controller TableView - How to reload MasterView Controller TableView from DetailViewController Swift 3 - 从不同的视图控制器过滤tableview - Swift 3 - filter tableview from different view controller 从后台打开应用程序时,iOS Swift 3重新加载TableView - iOS swift 3 reload tableview when open app from background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM