[英]Two collectionViews in one tableView Cell. collectionViews not appearing
I have two different collectionViews inside one table view cell. 我在一个表视图单元格中有两个不同的collectionViews。 Reason is because I am trying to find the best way to implement one horizontal collectionView and a vertical collecitonView that displays different data. 原因是因为我试图找到实现一个水平collectionView和一个显示不同数据的垂直collecitonView的最佳方法。 I just want to get the horizontal collectionView to scroll down with the page when users are scrolling with the vertical collection view. 当用户使用垂直集合视图滚动时,我只想让水平collectionView随页面向下滚动。
I have implemented some code to try to get both collectionViews working inside the table view, however the collection Views are not appearing. 我已经实现了一些代码,试图使两个collectionView在表视图中都可以工作,但是没有出现集合视图。
class PeopleToFollowHeader: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var collectionViewA: UICollectionView!
@IBOutlet weak var collectionViewB: UICollectionView!
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == self.collectionViewA {
return 6
} else {
return posts.count
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == self.collectionViewA {
let cellA = collectionView.dequeueReusableCell(withReuseIdentifier: "FollowCell", for: indexPath) as! PeopleToFollowCollectionCell
return cellA
} else {
if collectionView == self.collectionViewB {
let cellB = collectionView.dequeueReusableCell(withReuseIdentifier: "FollowingFeed", for: indexPath) as! FollowingCell
cellB.posts = posts[indexPath.item]
return cellB
}
return UICollectionViewCell()
}
}
override func awakeFromNib() {
super.awakeFromNib()
fetchPosts()
// Display collectionViews
collectionViewA.delegate = self
collectionViewA.dataSource = self
collectionViewB.delegate = self
collectionViewB.dataSource = self
self.addSubview(collectionViewA)
self.addSubview(collectionViewB)
collectionViewA.reloadData()
collectionViewB.reloadData()
}
```
You should reload your collection views after fetchPosts()
has returned (assuming it is an asynchronous function). 在fetchPosts()
返回之后(假设它是一个异步函数),您应该重新加载集合视图。
So you should have the data prefetched before reloading the whole tableView is reloaded. 因此,您应该在重新加载整个tableView之前预取数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.