TableView 分页问题

问题描述

我的应用从news-Api网站收集数据;当我滚动到屏幕底部时,它不会加载更多文章,而是执行反弹效果。以下是分页的逻辑。我该如何解决?请查看最新新闻和文章管理器项目链接https://github.com/lexypaul13/Covid-News/tree/master/Covid%20News

func performRequest(page:Int) {
        let endPoint = baseUrl + "&apiKey=d32071cd286c4f6b9c689527fc195b03&" + "pageSize=20&page=\(page)"
        guard let aritcleUrl = URL(string: endPoint) else {return}
        let request = URLRequest(url: aritcleUrl)
        let task = URLSession.shared.dataTask(with: request,completionHandler: { [weak self] (data,response,error) -> Void in //collects content from website
            guard let self = self else { return }
            if  error != nil { // checks if content is available
                print(error ?? 0)
                return
            }
            if let data = data {self.articles = self.parseData(data: data)}})
        task.resume()
    }
    
    
    
    func parseData(data: Data) -> [ArticlesData]? {
        do {
            let jsonResult = try JSONSerialization.jsonObject(with: data,options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
            let jsonArticles = jsonResult?["articles"] as? [AnyObject] ?? [] // gets first head of json file and converts it to dictionary
            for jsonArticle in jsonArticles { // captures data and stores it in the model object
                let article = ArticlesData()
                article.author = jsonArticle["author"] as? String
                article.myDescription = jsonArticle["description"] as? String
                article.publishedAt = jsonArticle["publishedAt"] as? String
                article.urlImage = jsonArticle["urlToImage"] as? String
                article.urlWebsite = jsonArticle["url"] as? String
                articles?.append(article) //put article data in the array
            }
            articles?.sort(by:{ $0.publishedAt! > $1.publishedAt! })
            let nc = NotificationCenter.default
            nc.post(name: Notification.Name("didFinishParsing"),object: nil)
        } catch {
            print("\(error)")
        }
        return articles ?? []
    }


    func scrollViewDidEndDragging(_ scrollView: UIScrollView,willDecelerate decelerate: Bool) {
            let offsetY         = scrollView.contentOffset.y
            let contentHeight   = scrollView.contentSize.height
            let height          = scrollView.frame.size.height
            if offsetY > contentHeight - height {
                news.performRequest(page: page)
                page+=1
            }
        }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)