swift - 分页时collectionView contentOffset发生变化 - swift - 以编程方式
问题描述
我正在使用 collectionView 构建聊天功能,这样做我正在实现分页:
但是,每当我向模型数组添加更多消息并重新加载 collectionView 数据时collectionView.reloadData()
,内容偏移量不受尊重,它会直接跳到顶部。
这是获取更多数据并将其加载到 collectionView 中的代码:
fileprivate func fetchMoreMessages(){
let db = Firestore.firestore()
db.collection("Messages").document(currentUserUid!).collection(chatUserUid!).order(by: "timestamp", descending: false).end(beforeDocument: firstDocumentSnapshot!).limit(to: 20).getDocuments { [self] (snapshot, error) in
if let error = error{
print("error in retrieving docs", error.localizedDescription)
return
}
let firstDoc = snapshot?.documents.first
self.firstDocumentSnapshot = firstDoc
var arrayOfDocuments : [MessageModel] = []
snapshot?.documents.forEach({ (snapshot) in
let dict = snapshot.data()
var message = MessageModel()
message.initiateMessageWith(dict: dict)
arrayOfDocuments.append(message)
})
self.listOfMessages.insert(contentsOf: arrayOfDocuments, at: 0)
self.collectionView.reloadData()
}
}
并在发生这种情况时被调用:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.y <= 100{
if self.isPaginating == false && ((firstDocumentSnapshot?.exists) != nil){
fetchMoreMessages()
self.isPaginating = true
}
}
}
解决方案
推荐阅读
- sql-server - 在 Django 中是否有办法始终将特定字段投射到 VARCHAR?
- flutter - 在 Windows 上编译 Flutter 引擎
- amazon-s3 - AWS S3 生命周期规则:仅删除根中的 .JSON 文件
- ansible - 使用 ansible 循环遍历列表字典
- constructor - Error "No value was passed to the mandatory parameter" in constructor of subclass
- python - ipywidgets.VBox not showing once part of function
- r - rlang:输入应该是符号和字符串
- php - Create an array with values for each loop through
- c# - How to insert current date and time on a specific column in sql using C#
- php - Using preg_replace() for template engines