合并2个类数组并根据日期在集合视图中追加

时间:2017-09-19 16:06:53

标签: arrays firebase swift3 uicollectionview

很抱歉这个问题之前已经被问到,但我还没有设法找到解决方案,所以我转发它以查看是否有其他人可以帮助我。

如何在集合视图中合并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
                }

1 个答案:

答案 0 :(得分: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
}