我怎么知道 reloadData 已经完成
How i know reloadData has finished
我有一个带有一些单元格的 TableView,这些单元格有一个用于开始 Activityindicator 的按钮,它会更新该单元格,但我试图仅在 reloadData() 完成时完成我的 ActivityIndicator,但答案是我看到 dispatch_async 不起作用,因为指示器先完成,然后更新 TableView
编辑
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
//From here
self.initOrEndMed = false
let myAlert = UIAlertController(title: "Alerta", message: "Tratamento finalizado", preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: {(alert: UIAlertAction!) -> Void in
self.loadingView(loadingText: "", on: false)
})
myAlert.addAction(okAction)
self.present(myAlert, animated: true, completion: nil)
//To here i want to do after reloadData has finished
})
这是我的 loadingView()
func loadingView(loadingText: String, on: Bool) {
self.labelForLoad.text = loadingText
self.labelForLoad.textAlignment = .center
self.labelForLoad.numberOfLines = 2
self.labelForLoad.sizeToFit()
self.labelForLoad.textColor = UIColor(red:0.24, green:0.69, blue:1.00, alpha:1.0)
self.labelForLoad.center = CGPoint(x: activityIndicator.center.x, y: activityIndicator.center.y + 40)
self.overlay.center = CGPoint(x: self.view.bounds.midX, y: self.view.bounds.midY)
self.overlay.addSubview(labelForLoad)
if on {
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(0.5)
overlay.alpha = overlay.alpha > 0 ? 0 : 0.95
UIView.commitAnimations()
self.view.isUserInteractionEnabled = false
activityIndicator.startAnimating()
} else {
self.view.isUserInteractionEnabled = true
overlay.alpha = 0
activityIndicator.stopAnimating()
}
}
没有任何方法可以告诉您 UITableView 已完成所有单元格的加载。解决方法是在您获得此数据源回调时检查 indexPath.row 是否是数据集中的最后一项 -
tableView(UITableView, cellForRowAt: IndexPath)
然后从 cellForRowAt: 方法中调用 loadingView()。
我有一个带有一些单元格的 TableView,这些单元格有一个用于开始 Activityindicator 的按钮,它会更新该单元格,但我试图仅在 reloadData() 完成时完成我的 ActivityIndicator,但答案是我看到 dispatch_async 不起作用,因为指示器先完成,然后更新 TableView
编辑
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
//From here
self.initOrEndMed = false
let myAlert = UIAlertController(title: "Alerta", message: "Tratamento finalizado", preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: {(alert: UIAlertAction!) -> Void in
self.loadingView(loadingText: "", on: false)
})
myAlert.addAction(okAction)
self.present(myAlert, animated: true, completion: nil)
//To here i want to do after reloadData has finished
})
这是我的 loadingView()
func loadingView(loadingText: String, on: Bool) {
self.labelForLoad.text = loadingText
self.labelForLoad.textAlignment = .center
self.labelForLoad.numberOfLines = 2
self.labelForLoad.sizeToFit()
self.labelForLoad.textColor = UIColor(red:0.24, green:0.69, blue:1.00, alpha:1.0)
self.labelForLoad.center = CGPoint(x: activityIndicator.center.x, y: activityIndicator.center.y + 40)
self.overlay.center = CGPoint(x: self.view.bounds.midX, y: self.view.bounds.midY)
self.overlay.addSubview(labelForLoad)
if on {
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(0.5)
overlay.alpha = overlay.alpha > 0 ? 0 : 0.95
UIView.commitAnimations()
self.view.isUserInteractionEnabled = false
activityIndicator.startAnimating()
} else {
self.view.isUserInteractionEnabled = true
overlay.alpha = 0
activityIndicator.stopAnimating()
}
}
没有任何方法可以告诉您 UITableView 已完成所有单元格的加载。解决方法是在您获得此数据源回调时检查 indexPath.row 是否是数据集中的最后一项 -
tableView(UITableView, cellForRowAt: IndexPath)
然后从 cellForRowAt: 方法中调用 loadingView()。