所以我有2个数组,1个是photoPost数组,另一个是videoPost数组。现在是否可以同时在一个集合视图中显示这两个数组?我尝试使用下面的方法,但没有成功。我不确定我是否做得对。我们将不胜感激。提前致谢
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)
})
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.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
cell. photoPost = photoPosts[indexPath.item]
cell.videoPost = videoPosts[indexPath.item] // fatal error:index out of range
return cell
}
答案 0 :(得分:1)
请试试这个
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
}