2017-09-19 96 views
0

對不起,這個問題以前已經問過,但我還沒有設法找到一個解決方案,所以我轉發它,看看是否有其他人可以幫助我。合併2類數組並在追蹤視圖中追加日期

如何在集合視圖中合併2個類數組?我已經設法顯示2個數組,但我怎樣才能讓它們交織在一起並根據postDate追加?

下面我有一個photoPosts數組和videoPosts數組,我目前使用下面的方法。但是這隻能顯示一個接一個的數組。在此先感謝

var photoPosts = [photoPost]() 
var videoPosts = [videoPost]() 

func retrieveData(){ 
let ref = Database.database().reference().child("videoPost").child(uid) 
     ref.observe(.childAdded, with: { (snapshot) in 
      let dictionary = videoPost(snapshot: snapshot) 
      self.videoPosts.append(dictionary) 
      self.videoPosts.sort(by: { (p1, p2) -> Bool in 
       return p1.postDate?.compare(p2.postDate!) == .orderedDescending 
       }) 
}) 
let ref = Database.database().reference().child("photoPost").child(uid) 
     ref.observe(.childAdded, with: { (snapshot) in 
      let dictionary = photoPost(snapshot: snapshot) 
      self. photoPosts.append(dictionary) 
       self.posts.sort(by: { (p1, p2) -> Bool in 
        return p1.postDate?.compare(p2.postDate!) == .orderedDescending 
       }) 
}) 
self.newsfeedCollectionView?.reloadData() 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
return videoPosts.count + photoPosts.count 
} 
     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {   
       let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! newsfeedCollectionViewCell 
      if indexPath.item < photoPosts.count { 
       cell. photoPost = photoPosts[indexPath.item] 
       } else { 
       cell.videoPost = videoPosts[indexPath.item-photoPosts.count] 
       } 
       return cell 
       } 

回答

0

請檢查以下內容:

var combinedArray = NSMutableArray() 

func retrieveData(){ 
    let ref = Database.database().reference().child("videoPost").child(uid) 
    ref.observe(.childAdded, with: { (snapshot) in 
     let dictionary = videoPost(snapshot: snapshot) 
     combinedArray.add(dictionary) 
    }) 

    let ref = Database.database().reference().child("photoPost").child(uid) 
    ref.observe(.childAdded, with: { (snapshot) in 
     let dictionary = photoPost(snapshot: snapshot) 
     combinedArray.add(dictionary) 
    }) 

    self.combinedArray.sort(by: { (p1, p2) -> Bool in 
     var postDate1: Date? 
     var postDate2: Date? 
     if let photoPost1 = p1 as? photoPost { 
      postDate1 = photoPost1.postDate 
     } else if let videoPost1 = p1 as? videoPost { 
      postDate1 = videoPost1.postDate 
     } 

     if let photoPost2 = p2 as? photoPost { 
      postDate2 = photoPost2.postDate 
     } else if let videoPost2 = p2 as? videoPost { 
      postDate2 = videoPost2.postDate 
     } 

     return postDate1!.compare(postDate2!) == .orderedDescending 
    }) 
    self.newsfeedCollectionView?.reloadData() 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return combinedArray.count 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! newsfeedCollectionViewCell 

    if let photoItem = combinedArray[indexPath.item] as? photoPost { 
     cell.photoPost = photoItem 
    } else { 
     cell.videoPost = combinedArray[indexPath.item] as videoPost 
    } 
    return cell 
} 
+0

感謝您的答覆。是否還有其他的東西我可以在''[]「'之間使用,因爲我得到一個錯誤:空集合文字需要顯式類型的行'var combinedArray = []' –

+0

我嘗試過'var combinedArray = [] as NSArray'但我仍然得到一個錯誤 –

+0

錯誤:空的集合文字需要明確的類型 –