我有一个聊天应用程序,正在其中以表格视图显示消息。当我拉动刷新并在顶部显示更多消息时,我希望保留在触发拉动刷新的同一屏幕上,即不跳转。以下代码无效,并且向下滚动。
func refresh() {
currentPage = currentPage + 1
if currentPage <= totalPages {
fetchMessaes(page: self.currentPage, completed: {
self.messageArray.append(contentsOf: self.fetchedMessages!.messages!)
// Sort message by ID so that latest message appear at the bottom.
let sortedArray = self.messageArray.sorted(by: {$0.id! < $1.id!})
self.fetchedMessages = sortedArray
self.messagesTable.reloadData()
// Scroll to the last row of recently loaded messages
DispatchQueue.main.async {
let index = IndexPath(row: 0, section: 0)
self.messagesTable.scrollToRow(at: index, at: .top, animated: false)
}
})
}
refreshControl.endRefreshing()
}
答案 0 :(得分:0)
能否请您将表视图reloadData()
放在main queue
内并尝试?
DispatchQueue.main.async {
self.messagesTable.reloadData()
let index = IndexPath(row: 0, section: 0)
self.messagesTable.scrollToRow(at: index, at: .top, animated: false)
}