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!