2017-10-12 3 views
0

collectionView의 맨 아래로 스크롤 할 수 있지만 때로는 잘못된 경로 예외가 발생합니다. numberOfSections 및 numberOfItemsInSection 검사를 시도했지만 오류가 가끔 나타나서 내 응용 프로그램이 충돌합니다. 또한 스크롤하려면 setContentOffset 메서드를 사용하여 시도했지만 여전히 오류가 가끔 나타납니다. 당신은유효하지 않은 경로 예외

 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

무엇 아마 무슨 일이 일어나고은 경쟁 조건이다 : 나는 또한이 시도

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

:

내가 collectionView의 하단으로 스크롤을 사용하고있는 코드입니다 self.collectionView?.reloadData()을 수행하고 바로 그 다음으로 collectionView을 스크롤하려고합니다. Item이 준비되지 않았거나 예외가 발생하는 경우도 있습니다.

또한 중첩 된 async 전화가있는 것으로 보입니다. 해야 할 일 :

  1. dispatch 중에서 self.collectionView?.reloadData()을 이동하십시오.
  2. 스크롤하려는 indexPath의 항목이 실제로는 scrollToItem을 수행하기 전에 존재하는지 확인하십시오. 마지막 ItemUICollectionViewDataSource 내에서 생성 한 다음 스크롤 동작을 실행 할 때마다

또 다른 방법 그 대신 내부 Firebase 전화를 스크롤이 될 수있다, 당신은 확인.

+0

마지막 항목이 있는지 확인하는 방법에 대한 코드 샘플을 제공해 주시겠습니까? UICollectionViewDataSource 내에서 생성 되었습니까? –

관련 문제