我可以滚动到collectionView的底部,但有时它会引发无效路径异常。我试图检查numberOfSections和numberOfItemsInSection,但错误仍然有时显示,我的应用程序崩溃。我也尝试使用setContentOffset方法滚动,但错误仍然有时显示。路径异常无效
这是我使用滚动到的CollectionView的底部代码:
guard let uid = FIRAuth.auth()?.currentUser?.uid, let toId = user?.id else {
return
}
let userMessagesRef = FIRDatabase.database().reference().child("user-messages").child(uid).child(toId)
userMessagesRef.observe(.childAdded, with: { (snapshot) in
guard let messageId = snapshot.key as? String else {
return
}
let messagesRef = FIRDatabase.database().reference().child("messages").child(messageId)
messagesRef.observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String: AnyObject] else {
return
}
self.messages.append(Message(dictionary: dictionary))
DispatchQueue.main.async(execute: {
self.collectionView?.reloadData()
//scroll to the last index
if self.messages.count > 0 {
if let indexPath = IndexPath(item: self.messages.count - 1, section: 0) as? IndexPath? {
self.collectionView?.scrollToItem(at: indexPath!, at: .bottom, animated: false)
}
}
})
}, withCancel: nil)
}, withCancel: nil)
}
我也试过这样:
DispatchQueue.main.async(execute: {
self.collectionView?.reloadData()
//scroll to the last index
if let _ = self.collectionView?.dataSource?.collectionView(self.collectionView!, cellForItemAt: IndexPath(row: 0, section: 0)) {
if let indexPath = IndexPath(item: self.messages.count - 1, section: 0) as? IndexPath? {
self.collectionView?.scrollToItem(at: indexPath!, at: .bottom, animated: false)
}
}
})
[滚动的可能的复制来的CollectionView的底部没有动画,没有无效的路径例外](https://stackoverflow.com/questions/46700716/scroll-to-bottom-of-collectionview-without-animation-and-without-invalid-path-ex) – jvrmed
对不起,这个问题没有回答,因此我有再发布一次!我正在删除这个问题。 –
如果你能分享关于崩溃消息错误的详细信息(打印,日志等),它将更容易引导你某处 – jvrmed