我对更多页面有数据响应,目前正在加载第1页,仅包含20条记录。
当我呼叫Api时,我给出的第1页如下:
func getMovies()
{
API.getNewMovies(page: 1, completion: {movies in
self.nowPlayingMovies = movies
self.tableView.reloadData()
})
}
但是我想使第No页变成动态的,并根据页码加载更多数据。
我知道如何检测何时到达最后一行数据。但是到达后,我想增加页面编号并加载下一页的更多数据以及现有数据。
我尝试如下所示。.但我知道这是错误的,无法完全正常工作。
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
// Check if the last row number is the same as the last current data element
if indexPath.row == self.nowPlayingMovies.count - 1 {
var pageNum:Int = 1
pageNum += 1
print(pageNum)
// self.loadMore()
}
}
func loadMore()
{
print("lastItem Reached")
// let lastItem = nowPlayingMovies.last!
// print("lastItem \(lastItem)")
// What Should I write here to load more data along with current data
}
import Foundation
struct APIResults: Decodable {
let page: Int
let numResults: Int
let numPages: Int
let movies: [Movie]
private enum CodingKeys: String, CodingKey {
case page, numResults = "total_results", numPages = "total_pages", movies = "results"
}
}
struct Movie: Decodable {
let id:Int!
let posterPath: String
var videoPath: String?
let backdrop: String
let title: String
var releaseDate: String
var rating: Double
let overview: String
private enum CodingKeys: String, CodingKey {
case id, posterPath = "poster_path", videoPath, backdrop = "backdrop_path", title, releaseDate = "release_date", rating = "vote_average", overview
}
}
我正在使用数据模型。请给我解释一些清晰的代码段。
如果您想解释更多代码,请告诉我。我将发布所需的代码。谢谢。
答案 0 :(得分:1)
将pageNumber作为参数传递,并将影片附加到数据源。
var lastPageRetrieved: Int
var nowPlayingMovies: [Movie] = []
var atTheEnd: Bool = false
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
// Check if the last row number is the same as the last current data element
if indexPath.row == self.nowPlayingMovies.count - 1 {
// Last item reach, load the next page if we're not at the end
if !atTheEnd {
self.loadMore()
}
}
}
func loadMore() {
// Get the next page number
var nextPage: Int = 1
if lastPageRetrieved > 0 {
nextPage += lastPageRetrieved
}
// Call the API with the right page number
getMovies(page: nextPage)
}
func getMovies(page: Int) {
API.getNewMovies(page: page, completion: {movies in
guard let movies = movies else {
// The movies were not loaded, are we at the end? You can make this even
// smarter by checking the number of records in the page and if the records
// are less than the max page size, we'll know this is the last page
self.atTheEnd = true
return
}
// Append the new movies to the data source
self.nowPlayingMovies += movies
// Record the last page you retrieved
self.lastPageRetrieved = page
// Reload your tableView with the new movies
self.tableView.reloadData()
})
}
您需要存储上一次检索的页面,并在获取页面数据后进行设置。另外,请考虑在API无法返回或到达最后一页时进行错误处理。
更新:我对代码进行了编辑,使其更加健壮。
答案 1 :(得分:1)
使用方法如下
func loadMore()
{
print("lastItem Reached")
let startingPageIndex = self.nowPlayingMovies.count + 1 // get the index
getMoview(startingPageIndex: startingPageIndex) // call load movies api
}
func getMovies(startingPageIndex: Int) {
API.getNewMovies(page: startingPageIndex, completion: {movies in
if self.nowPlayingMovies.count > 0 {
self.nowPlayingMovies += movies
} else {
self.nowPlayingMovies = movies
}
self.tableView.reloadData()
})
}