我在一个表格 View 单元格中有两个不同的 collectionView。原因是因为我试图找到实现一个水平 collectionView 和一个显示不同数据的垂直 collecitonView 的最佳方法。当用户使用垂直 Collection View 滚动时,我只想让水平 collectionView 随页面向下滚动。
我已经实现了一些代码来尝试让两个 collectionViews 在 TableView 中工作,但是 Collection View 没有出现。
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
} khác {
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
} khác {
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()
}
```
您应该在 fetchPosts()
返回后重新加载您的 Collection View (假设它是一个异步函数)。
因此您应该在重新加载整个 tableView 之前预取数据。
Tôi là một lập trình viên xuất sắc, rất giỏi!