ios - 在 Swift 中为 tableView 进行分页时,ScrollView 被击中
问题描述
我在滚动视图中添加了动态高度表视图。我已经为 tableview 实现了分页,即当用户加载页面时,api 将被调用,当用户到达 scrollView 的末尾时,我正在调用另一个 Api,然后调用 tableView Reload() 方法。滚动到达末尾直到 tableview 重新加载应用程序的问题是 scrollView 被击中并且无法单击应用程序上的任何位置
override func viewWillAppear(_ animated: Bool) {
self.tbl.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if(scrollview.contentOffset.y >= (scrollview.contentSize.height - scrollview.frame.size.height))
{
//user has scrolled to the bottom
spinner.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: tbl.bounds.width, height: 40)
//spinner.transform = CGAffineTransform(scaleX: 2, y: 2)
self.tbl.tableFooterView = spinner
self.tbl.tableFooterView?.backgroundColor = UIColor.white
self.tbl.tableFooterView?.isHidden = false
if dataLoading == false{
spinner.startAnimating()
loadMoreActivities()
}
}
}
func loadMoreActivities(){
DispatchQueue.global(qos: .background).async {
self.PageNo += 1
self.dataLoading = true
ApiService.Call(endPoint: API , params: params, httpMethod: method,header: ApiService.GetHeader_Auth(),informSuccess: false,encoding: .httpBody,vcSender: self,shouldAnimate: true) { (Result : JSON) in
if !ApiService.IsResponseOK(result: Result)
{
self.spinner.stopAnimating()
return
}
self.tbl.tableFooterView?.isHidden = true
if let data = try? Result["Data"].rawData()
{
self.listOfActivities = try? JSONDecoder().decode(Activities.self, from: data)
DispatchQueue.main.async {
self.tbl.ForceReload()
}
self.spinner.stopAnimating()
}
}
}
}
解决方案
在方法中的主线程上完成所有UI
工作,loadMoreActivities
func loadMoreActivities(){
DispatchQueue.global(qos: .background).async {
self.PageNo += 1
self.dataLoading = true
ApiService.Call(endPoint: API , params: params, httpMethod: method,header: ApiService.GetHeader_Auth(),informSuccess: false,encoding: .httpBody,vcSender: self,shouldAnimate: true) { [weak self] (Result : JSON) in
guard let self = self else {
DispatchQueue.main.async {
self?.spinner.stopAnimating()
}
return
}
if !ApiService.IsResponseOK(result: Result)
{
DispatchQueue.main.async {
self.spinner.stopAnimating()
}
return
}
DispatchQueue.main.async {
self.tbl.tableFooterView?.isHidden = true
}
if let data = try? Result["Data"].rawData()
{
self.listOfActivities = try? JSONDecoder().decode(Activities.self, from: data)
DispatchQueue.main.async {
self.tbl.ForceReload()
self.spinner.stopAnimating()
}
}
}
}
}
推荐阅读
- java - Android HttpURLConnection POST 不工作。使用 C#
- vue.js - 解析未定义的道具?
- flutter - TextButton的文本颜色没有改变
- javascript - 如何使 JavaScript 中添加的元素与 HTML 中添加的元素反应相同?
- generics - 尽可能实现 Into,否则使用 TryInto
- apache-spark - 如何在pyspark中每7次创建一个索引列
- python - 基于单列在熊猫中创建一系列数字
- python - python-telegram-bot 发送消息而不使用更新和上下文
- postgresql - 从 oracle pl/sql 批量收集限制的 postgres 替代方案
- node.js - 如何使用 mongoose 或 mongodb 查询更改架构内方案数组中的值。?