Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift-重新加载数据UITableView进入前台_Ios_Swift_Uitableview - Fatal编程技术网

Ios Swift-重新加载数据UITableView进入前台

Ios Swift-重新加载数据UITableView进入前台,ios,swift,uitableview,Ios,Swift,Uitableview,当我返回前台时,我想调用reloadData方法TableView。我的TableView是以编程方式构建的 在FirstViewController中,我有方法填充TableView和函数reloadListOfApps,在这里我调用reloadData方法 class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, MFMailComposeViewControllerDel

当我返回前台时,我想调用
reloadData
方法
TableView
。我的
TableView
是以编程方式构建的

FirstViewController
中,我有方法填充
TableView
和函数
reloadListOfApps
,在这里我调用
reloadData
方法

class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, MFMailComposeViewControllerDelegate {     

    @IBOutlet weak var listOfApps: UITableView!

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

    func reloadListOfApps() {
        listOfApps.reloadData()
    }    

// ================ TABLE DELEGATE/MANAGER ===================

let apps = ListAppClass.listApp()

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return apps!.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()

    let bundle = apps! [indexPath.row]
    let bundle_temp = String(describing: bundle)

    cell.textLabel?.text = bundle_temp
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("Current Cell!")

}
}
AppDelegate
中,我

func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    FirstViewController().reloadListOfApps()
}
当我运行应用程序时,我发现了这个错误

致命错误:在展开可选值时意外发现nil

在阅读中,我还检查了故事板中的插座,似乎一切正常。

Hooks
对应于
FirstViewController


错误在哪里?

尝试在视图控制器中添加oberver

NotificationCenter.default.addObserver(self,
selector: #selector(applicationWillEnterForeground),
name: .UIApplicationWillEnterForeground,
object: nil)
回拨

@objc func applicationWillEnterForeground() {

     listOfApps.reloadData()

}
////问题解释

在AppDelegate中编写此代码时

  FirstViewController().reloadListOfApps()

这会动态创建一个实例,其所有属性均为零,因为您没有加载与当前活动对象关联的storybaord对象或Xib文件,控件转到reloadListOfApps函数,发现要重新加载的listOfApps为零,因此会发生崩溃,上述解决方案是一种方法,另一种方法是使用委托或使listOfApps成为可在任何位置引用的共享对象

尝试在视图控制器中添加oberver

NotificationCenter.default.addObserver(self,
selector: #selector(applicationWillEnterForeground),
name: .UIApplicationWillEnterForeground,
object: nil)
回拨

@objc func applicationWillEnterForeground() {

     listOfApps.reloadData()

}
////问题解释

在AppDelegate中编写此代码时

  FirstViewController().reloadListOfApps()

这会动态创建一个实例,其所有属性均为零,因为您没有加载与当前活动对象关联的storybaord对象或Xib文件,控件转到reloadListOfApps函数,发现要重新加载的listOfApps为零,因此会发生崩溃,上述解决方案是一种方法,另一种方法是使用委托或使listOfApps成为可在任何地方引用的共享对象

您的错误可能在这里
let bundle=apps![indexPath.row]
应用程序!。计数
检查应用程序是否为
nil
,无论如何,在
应用程序中实例化的
FirstViewController
将进入前台
它与导航堆栈中的另一个不同您的错误可能在这里
let bundle=apps![indexPath.row]
应用程序!。计数
检查应用程序是否为
nil
,无论如何,在
应用程序中实例化的
FirstViewController将进入前台
它与导航堆栈中的另一个不同,并带有您的修复(和很好的解释),现在应用程序不再崩溃,但不会重新加载数据。
TableView
是相同的。如果我关闭并重新启动应用程序,则表会更新。可能是拉操作失败,请尝试在后台添加虚拟数据并选中修复。它使用
apps
填充表,该表提供从中捕获数据的
Array
。但它总是一样的,因为超出了范围。现在我把它放对地方了!Tnx!有了你的修复(和很好的解释),现在应用程序不再崩溃,但不会重新加载数据。
TableView
是相同的。如果我关闭并重新启动应用程序,则表会更新。可能是拉操作失败,请尝试在后台添加虚拟数据并选中修复。它使用
apps
填充表,该表提供从中捕获数据的
Array
。但它总是一样的,因为超出了范围。现在我把它放对地方了!Tnx!