简体   繁体   English

从另一个ViewController Swift 4.2重新加载tableView

[英]reload tableView from Another ViewController Swift 4.2

when i am trying to call method from another VC reloadData() then the app crash Fatal error: Unexpectedly found nil while unwrapping an Optional value due to tablview nil how can resolve it. 当我试图从另一个VC reloadData()调用方法然后应用程序崩溃Fatal error: Unexpectedly found nil while unwrapping an Optional value由于tablview nil Fatal error: Unexpectedly found nil while unwrapping an Optional valueFatal error: Unexpectedly found nil while unwrapping an Optional value nil如何解决它。

FavoritesFiltersViewController.shareInstance.reloadData()

在此输入图像描述

Check the init function that's triggered when you call FavoritesFiltersViewController.shareInstance It seems that you are not giving a value to the tableView property. 检查调用FavoritesFiltersViewController.shareInstance时触发的init函数看来你没有给tableView属性赋值。

Just to check, you can remove the optional from it and see if the compiler complains because it has no initial value. 只是要检查,你可以从中删除可选项,看看编译器是否抱怨,因为它没有初始值。

  • You can use notificationCenter for the reload tableView data from 您可以使用notificationCenter从中重新加载tableView数据
    another viewController like below : 另一个viewController如下:

1. You can add this line to another viewController. 1.您可以将此行添加到另一个viewController。

 NotificationCenter.default.post(name: NSNotification.Name(rawValue: "newDataNotif"), object: nil)

2. Add this line(viewDidLoad) to viewController where contain tableView. 2.将此行(viewDidLoad)添加到包含tableView的viewController。

NotificationCenter.default.addObserver(self, selector: #selector(self.refresh), name: NSNotification.Name(rawValue: "newDataNotif"), object: nil)

3. Add this selector method for reload tableView data. 3.添加此选择器方法以重新加载tableView数据。

 @objc func refresh() {

    self.tblview.reloadData() // a refresh the tableView.

}

This is working fine. 这工作正常。 Thank you. 谢谢。

Thanks for every one.Finally after a R&D. 感谢每一个人。最后研发后。 i found the following solution. 我找到了以下解决方案。 and its working fine. 它的工作正常。

let fav:FavoritesFiltersViewController! 
 fav.reloadData()

暂无
暂无

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

相关问题 Swift - 从另一个 ViewController 在 TableView 中插入项目 - Swift - insert item in TableView from another ViewController 如何在Swift中从其他ViewController重新加载TableView中的数据 - How to reload data in a TableView from a different ViewController in Swift Swift:将数据从ViewController中的tableView传递到另一个ViewController - Swift: Passing data from a tableView in a ViewController to another ViewController 从另一个ViewController的Objective-C重载TableView - Objective-c reload tableview from another viewcontroller Swift - 在关闭 Modal ViewController 后重新加载 TableView - Swift - Reload TableView After Dismissing Modal ViewController Swift · Dismissing Modal ViewController and reload tableview after - Swift · Dismissing Modal ViewController and reload tableview afterwards 如何从 viewcontroller1 (Swift) 在 vi​​ewcontroller2 中附加列表和重新加载 tableView - How to append list and reload tableView in viewcontroller2 from viewcontroller1 (Swift) 如何在当前viewcontroller中重新加载另一个uiviewcontroller的tableview - how to reload tableview of another uiviewcontroller in current viewcontroller 无法从Swift中的TableView在另一个ViewController中显示值 - Not able to display values in another viewcontroller from TableView in Swift Swift:将数据从tableView显示到另一个ViewController(JSON,Alamorife,AlamofireImage) - Swift: Show data from tableView to another ViewController (JSON, Alamorife, AlamofireImage)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM