我是 swift 的新手,我找不到如何同时刷新 TableView 和 JSON 数据。刷新TableView后先刷新Json。我怎样才能做到这一点 ?
最佳答案
您需要将拉动刷新类添加到您的UITableView
private let refreshControl = UIRefreshControl()
var tableView: UITableView!
然后在ViewDidLoad
中将其作为 subview 添加到tableView
中,您甚至可以更改其颜色和文本。
override func viewDidLoad() {
if #available(iOS 10.0, *) {
tableView.refreshControl = refreshControl
} else {
tableView.addSubview(refreshControl)
}
refreshControl.tintColor = UIColor.blue
refreshControl.attributedTitle = NSAttributedString(string: "Fetching Data ...", attributes: [NSAttributedStringKey.foregroundColor : UIColor(red:0/255, green:168/255, blue:225/255, alpha:1.0)])
refreshControl.addTarget(self, action: #selector(refreshData(_:)), for: .valueChanged)
}
当值发生变化时,使用选择器函数向其添加目标,如上所示。
@objc private func refreshData(_ sender: Any) {
//load the data from the server and
//parse and put it in an array then reload your table view
}
关于json - 如何使用按钮或拉动刷新来刷新 TableView 和 JSON 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48470621/