2017-10-12 127 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

[滾動的可能的複製來的CollectionView的底部沒有動畫,沒有無效的路徑例外](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) 
       } 
      } 
     } 

} 
0

什麼是可能發生的是一個競爭條件,你正在做self.collectionView?.reloadData(),並立即嘗試將collectionView滾動到底部。有時項目還沒有準備好,這就是爲什麼你會得到一個例外。

此外,似乎你有一些嵌套的async調用。你應該做什麼:

  1. 移動self.collectionView?.reloadData()dispatch
  2. 檢查您試圖滾動到的indexPath的項目是否在做scrollToItem之前確實存在。

另一種方法可能是,與其在你裏面滾動Firebase電話,你只要檢查最後ItemUICollectionViewDataSource內創建並執行滾動操作。

+0

你能否給我一個如何檢查最後一項的代碼示例是在UICollectionViewDataSource中創建? –