gpt4 book ai didi

ios - 如何通过按钮重新获得互联网后重新加载tableView

转载 作者:行者123 更新时间:2023-11-30 11:40:59 25 4
gpt4 key购买 nike

我在tableViewController中设置了当用户失去互联网连接时的所有错误处理代码。假设用户退出单元格然后返回,ProgressHUD(自定义事件指示器)将从 viewDidAppear 中弹出,并且不会显示任何单元格,因为它们没有任何单元格无线上网。之后,假设用户重新获得连接,但 tableView 仍然是空的,我在导航栏中有一个按钮,可以从 UIApplicationDidBecomeActive 重新加载数据,但唯一的问题是 tableView 没有更新,仍然没有单元格显示。

@IBAction func tableReload(_ sender: Any) {              
if reach.connection == .wifi || reach.connection == .cellular {
NotificationCenter.default.addObserver(self, selector: #selector(reloadTable(note:)), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)

}else {
ProgressHUD.showError("Can not reload if there is no internet connection")// activity indicator from a library
}
}

@objc func reloadTable(note: NSNotification) {
let reachability = note.object as! Reachability

if reachability.connection == .wifi || reachability.connection == .cellular{
ProgressHUD.showSuccess("Wifi is good")
self.tableView.isScrollEnabled = true
self.tableView.isUserInteractionEnabled = true
self.tableView.reloadData()
}else {
tableView.isScrollEnabled = false
tableView.isUserInteractionEnabled = false
ProgressHUD.show("Can not reload with no internet connection")
}
}

最佳答案

@objc func reloadTable(note: NSNotification) {
let reachability = note.object as! Reachability

if reachability.connection == .wifi || reachability.connection == .cellular{
ProgressHUD.showSuccess("Wifi is good")
self.tableView.isScrollEnabled = true
self.tableView.isUserInteractionEnabled = true
self.fetchData() // call fetch Method
}else {
tableView.isScrollEnabled = false
tableView.isUserInteractionEnabled = false
ProgressHUD.show("Can not reload with no internet connection")
}
}

func fetchData() {
// Assign data to TableView's DataSource
// Now reload the TableView
}

关于ios - 如何通过按钮重新获得互联网后重新加载tableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49291439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com