gpt4 book ai didi

ios - UITableView使用willDisplay拉动刷新和分页

转载 作者:行者123 更新时间:2023-11-30 11:03:39 28 4
gpt4 key购买 nike

这是我的第三天,在阅读了几乎超过几十篇文章后,我不知道如何使用 willDisplay 方法在 UITableview 中实现分页。我正在尝试模仿 iMessages 拉动分页功能。

当我第一次拉刷新时,下面的代码会进入无限循环。

任何人都可以查看代码并提出解决无限加载的方法吗?

提前发送。

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

if indexPath.row == 0 && !self.isLoading {

self.isLoading = true

fetchClientsMessages(page: self.currentPage, completed: {

if self.currentPage == 0 {
self.messageArray.removeAll()
}

self.messageArray.append(contentsOf: self.clientChatMessages!.messages!)

// Sort message by ID so that latest message appear at the bottom.
self.messageArray = self.messageArray.sorted(by: {$0.id! < $1.id!})

self.messagesTable.reloadData()

// Scroll to the top
self.messagesTable.scrollToRow(at: indexPath, at: UITableViewScrollPosition.top, animated: true)

self.lastPage = self.currentPage

self.currentPage = self.currentPage + 1
self.isLoading = false
})

}

}

最佳答案

首先加载 if 将运行的代码,然后在其回调中执行

self.messagesTable.scrollToRow(at: indexPath, at: UITableViewScrollPosition.top, animated: true)
self.isLoading = false

这将再次触发 willDisplay 作为 indexPath = 0isLoading = false => 无限加载,你想要的是稍微向下滚动表格指示数据已加载,然后让用户向上滚动,直到再次点击 index = 0

关于ios - UITableView使用willDisplay拉动刷新和分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52992179/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com