首页 > 解决方案 > 在 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()
             }
           }
         }
      }

标签: iosswiftuitableview

解决方案


在方法中的主线程上完成所有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()
                }
             }
           }
         }
      }

推荐阅读