简体   繁体   English

如何使用标题水平滚动集合视图

[英]How to scroll the collection view horizontal with header

I have collectionview with header. 我有标头的collectionview。

This is the image 这是图像

In this, i have display the questions in the UICollectionReusableView as header and answers in the collectionviewcell. 在此,我在UICollectionReusableView中将问题显示为标题,并在collectionviewcell中显示答案。 So the image is this way.But i need to scroll the collectionview horizontal only. 所以图像是这种方式。但是我只需要水平滚动collectionview。

But that time it will become as :- 但是到那时它将变成:-

This is the header. 这是标题。

This is the cell. 这是牢房。

But i need this in the first screen header and its answers. 但是我需要在第一个屏幕标题及其答案中使用它。 when user scroll it then need show 2nd question in header and its answers. 当用户滚动时,则需要在标题及其答案中显示第二个问题。 How to implement it? 如何执行呢?

currently my code is as below:- 目前我的代码如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM