ios - 如何使用标题水平滚动集合视图
问题描述
我有带有标题的collectionview。
在此,我将 UICollectionReusableView 中的问题显示为标题,并在 collectionviewcell 中显示答案。所以图像是这样的。但我只需要水平滚动collectionview。
但到时候它会变成:-
但我需要在第一个屏幕标题及其答案中使用它。当用户滚动它时,需要在标题中显示第二个问题及其答案。如何实施?
目前我的代码如下: -
extension NH_DummyDataViewController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return self.questionViewModel.numberOfSections()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.questionViewModel.numberOfRowsIn(section: section)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = verticalCollectionView.dequeueReusableCell(withReuseIdentifier: "NH_Headercell", for: indexPath)as! NH_Headercell
// cell.options.text = "hello"
// let optionModel = self.questionViewModel.datafordisplay(atindex: indexPath)
cell.setReviewData(reviews:self.questionViewModel.datafordisplay(atindex: indexPath))
return cell
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let firstheader: NH_QuestionHeaderCell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "NH_QuestionHeaderCell", for: indexPath ) as! NH_QuestionHeaderCell
firstheader.setReviewData(reviews:questionViewModel.titleForHeaderInSection(atsection:indexPath.section))
return firstheader
}
}
// MARK: UICollectionViewDelegateFlowLayout
extension NH_DummyDataViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 100)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 100)
}
}
解决方案
设置集合视图滚动方向水平并设置分页
yourCollectionview.pagingEnabled = true
推荐阅读
- javascript - 一个函数如何检测我给出的是 HEX 还是 RGB 并因此将其从一个转换为另一个?
- javascript - 如何在离子应用程序中显示隐藏部分文本?
- lua - Lua:保存和打开按键索引的二维表
- android - 工作图未知时的 WorkManager 链接
- python - 在 Pandas 中将 8 位整数转换为标准日期格式
- c - 两个相似的 for 循环给出不同的结果
- c# - 模拟基类受保护的属性
- c# - 在 Unity 和 C# 中跨项目重用函数
- rust - 将 actix-web 作为 Windows 服务运行时出现问题
- django - Django 文档转换为 HTML