我可以滾動到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