2016-07-04 91 views
0

所以我當前的代碼根本不會滾動列表,而不管messageCount返回的數字是多少。它位於viewDidLoad()中,數據由Firebase填充。這是什麼造成的?如何滾動到UICollectionView的底部?

目前代碼:

self.messages.removeAll() 
    collectionView?.reloadData() 
    refHandle = self.ref.child("messages").observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in 
     self.messages.append(snapshot) 
     self.collectionView?.insertItemsAtIndexPaths([NSIndexPath(forItem: self.messages.count - 1, inSection: 0)]) 
    }) 

    refHandle = ref.child("messages").observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in 
     let messageCount = Int(snapshot.childrenCount) 
     print(messageCount) //temp 
     self.collectionView?.scrollToItemAtIndexPath(NSIndexPath(forItem: messageCount - 1, inSection: 0), atScrollPosition: .Bottom, animated: true) 
    }) 
+0

嘗試調用它在主線程 – Chris

回答

0

試試這個

```

DispatchQueue.main.async(execute: { 
       self.collectionView?.reloadData() 
       self.scrollToBottom() 
      }) 

``` 的觀察封閉

內,這是滾動到底部func

```

```