首页 > 解决方案 > 如何使用标题水平滚动集合视图

问题描述

我有带有标题的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)
    }
}

标签: iosuicollectionview

解决方案


设置集合视图滚动方向水平并设置分页

yourCollectionview.pagingEnabled = true

推荐阅读