2017-10-12 81 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中创建? –