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)
}
}
})
可能な複製例外](https://stackoverflow.com/questions/46700716/scroll-to-bottom-of-collectionview-without-animation-and-without-invalid-path-ex) – jvrmed
申し訳ありません、その質問は答えられていませんでしたこれを再び投稿する!私はその質問を削除しています。 –
クラッシュメッセージのエラー(印刷、ログなど)の詳細を共有できれば、あなたをどこかに導くことが容易になります – jvrmed