如何解决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 举报,一经查实,本站将立刻删除。