以下是我的代码,其中下拉菜单顶部加载了更多内容。当我到达最后一页并再次拉动时,它会继续调用fetchMessages。达到页数限制时,如何停止显示加载更多单元格的代码并停止获取更多数据?
class ViewController: UIViewController {
@IBOutlet weak var messagesTable: UITableView!
private var messageArray: [String] = []
let pageSize: Int = 20
let totalPages: Int = 3
var currentPage: Int = 0
var isLoading: Bool = false
var hasMore: Bool = true
private func loadMoreMessages() {
if self.isLoading {
return
}
if self.currentPage < 1 {
self.loadMessagesFirstTime()
return
}
self.isLoading = true
fetchMessages(page: self.currentPage + 1) { (messages) in
if messages.count > 0 {
for message in messages {
self.messageArray.insert(message, at: 0)
}
self.messagesTable.reloadData()
let lastIndexPath = IndexPath(row: messages.count - 1, section: 0)
self.messagesTable.scrollToRow(at: lastIndexPath, at: UITableViewScrollPosition.top, animated: false)
self.currentPage += 1
} else {
self.hasMore = false
self.messagesTable.reloadData()
}
self.isLoading = false
}
}
private func loadMessagesFirstTime() {
print("loadMesagesFirstTime")
self.isLoading = true
self.hasMore = true
self.currentPage = 1
fetchMessages(page: 1) { (messages) in
self.messageArray.removeAll()
if messages.count > 0 {
self.messageArray.append(contentsOf: messages.reversed())
self.messagesTable.reloadData()
let lastIndexPath = IndexPath(row: self.messagesTable.numberOfRows(inSection: 0) - 1, section: 0)
self.messagesTable.scrollToRow(at: lastIndexPath, at: UITableViewScrollPosition.bottom, animated: false)
} else {
self.hasMore = false
self.messagesTable.reloadData()
}
self.isLoading = false
}
}
}
extension ViewController {
func fetchMessages(page: Int, completed: @escaping ([String]) -> Swift.Void) {
print("fetchMessages, page: \(page)")
DispatchQueue.global().async {
sleep(1)
DispatchQueue.main.async {
var messages: [String] = []
if page <= self.totalPages {
for index in 0..<self.pageSize {
messages.append("Row \((page-1) * self.pageSize + index)")
}
}
completed(messages)
}
}
}
}
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.hasMore {
return self.messageArray.count + 1
} else {
return self.messageArray.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var row = indexPath.row
if self.hasMore {
if row == 0 {
let reusableIdentifier = "LoadingCell"
let cell = tableView.dequeueReusableCell(withIdentifier: reusableIdentifier) ??
UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: reusableIdentifier)
cell.textLabel?.text = "Loading ..."
return cell
}
row -= 1
}
let reusableIdentifier = "MessageTableViewCell"
let cell = tableView.dequeueReusableCell(withIdentifier: reusableIdentifier) ??
UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: reusableIdentifier)
cell.textLabel?.text = self.messageArray[row]
return cell
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
loadMoreMessages()
}
}
}
输出:
loadMesagesFirstTime
fetchMessages,页面:1
fetchMessages,页面:2
fetchMessages,第3页
fetchMessages,第4页:<==我们不应该调用它 页数限制。
fetchMessages,第4页
fetchMessages,第4页
fetchMessages,第4页
fetchMessages,第4页
答案 0 :(得分:-1)
尝试这个:
if page < self.totalPages