2017-10-12 14 views
0

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) 
       } 
       } 
      }) 
+0

可能な複製例外](https://stackoverflow.com/questions/46700716/scroll-to-bottom-of-collectionview-without-animation-and-without-invalid-path-ex) – jvrmed

+0

申し訳ありません、その質問は答えられていませんでしたこれを再び投稿する!私はその質問を削除しています。 –

+0

クラッシュメッセージのエラー(印刷、ログなど)の詳細を共有できれば、あなたをどこかに導くことが容易になります – jvrmed

答えて

0

IndexPathはそうあなたがこのような何かを行うことができますvar numberOfSections: Int { get }func numberOfItems(inSection section: Int) -> Int

を使用して正しいかどうかをチェックすることができます:

DispatchQueue.main.async { 

     self.collectionView?.reloadData() 
     if self.collectionView?.numberOfSections > 0 { 
      //you can decide which section to choose if you have more than one section but I am here using 0 for now... 
      let numberOfItemsInSection = self.collectionView?.numberOfItems(inSection: 0) 

      if numberOfItemsInSection > 0 { 
       if let indexPath = IndexPath(item: numberOfItemsInSection - 1, section: 0) as? IndexPath { 
        self.collectionView?.scrollToItem(at: indexPath, at: .bottom, animated: false) 
       } 
      } 
     } 

} 
アニメーションがなく、無効なパスなしCollectionViewの一番下に[スクロールの
0

は、競合状態で、あなたをself.collectionView?.reloadData()を実行していて、その直後にcollectionViewをスクロールしようとしています。アイテムがまだ準備ができていないことがあり、例外が発生することがあります。

さらに、ネストしたasyncコールがあるようです。何をすべきか:

  1. dispatchのうちself.collectionView?.reloadData()を移動してください。
  2. scrollToItemを実行する前にスクロールしようとしているindexPathの項目が実際に存在するかどうかを確認します。

別のアプローチではなく、あなたの中Firebaseコールをスクロールする、あなたが最後ItemUICollectionViewDataSource内に作成されるたびにチェックして、スクロールアクションを実行しているかもしれません。

+0

最後の項目がUICollectionViewDataSource内に作成されますか? –

関連する問題