Ios 具有多个不同集合视图单元格的集合视图

Ios 具有多个不同集合视图单元格的集合视图,ios,swift,xcode,uicollectionview,Ios,Swift,Xcode,Uicollectionview,我正在构建一个调查应用程序,我想使用这样的视图 在这个例子中,只有两个答案是可能的,但我希望有3/4的答案和不同类型的调查 我将收到一个JSON,用问题类型和可能的答案等构建它 我的主要问题是,我真的不知道如何继续,我是iOS开发atm的新手,我不想尝试太多东西,也不想使用意大利面代码,所以如果有人知道如何根据json使用不同的视图进行此集合视图 谢谢你,有很多方法可以实现这一点,但正如你所想的那样,你需要使用带有多个不同集合视图单元格的集合视图 在ViewController.swift中,遵

我正在构建一个调查应用程序,我想使用这样的视图

在这个例子中,只有两个答案是可能的,但我希望有3/4的答案和不同类型的调查

我将收到一个JSON,用问题类型和可能的答案等构建它

我的主要问题是,我真的不知道如何继续,我是iOS开发atm的新手,我不想尝试太多东西,也不想使用意大利面代码,所以如果有人知道如何根据json使用不同的视图进行此集合视图


谢谢你,有很多方法可以实现这一点,但正如你所想的那样,你需要使用带有多个不同集合视图单元格的集合视图

在ViewController.swift中,遵循UICollectionView协议

    class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate , UICollectionViewDelegateFlowLayout{

        @IBOutlet weak var mainCollectionView: UICollectionView!

        override func viewDidLoad() {
            super.viewDidLoad()
             mainCollectionView.isScrollEnabled = false
        }
然后实现集合视图数据源和委托方法。

// MARK: - UICollectionViewDataSource protocol

// tell the collection view how many cells to make
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 3
}


// make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {



    print("indexPath.row \(indexPath.row)")
    if indexPath.row == 0{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "firstCell", for: indexPath)
        if let button = cell.contentView.viewWithTag(2) as? UIButton{
            button.addTarget(self, action: #selector(MoveToNextCell), for: .touchUpInside)
        }
        return cell
    }

    else if indexPath.row == 1{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "secondCell", for: indexPath)
        if let button = cell.contentView.viewWithTag(3) as? UIButton{
            button.addTarget(self, action: #selector(MoveToNextCell), for: .touchUpInside)
        }

        return cell
    }

    else{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "thirdCell", for: indexPath)
        if let button = cell.contentView.viewWithTag(4) as? UIButton{
            button.addTarget(self, action: #selector(MoveToNextCell), for: .touchUpInside)
        }
        return cell
    }
}


// MARK: - UICollectionViewDelegate protocol

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    // handle tap events
    print("You selected cell #\(indexPath.item)!")
}
func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {

            return CGSize(width: self.view.frame.width , height: self.view.frame.height)


    }

    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 1.0
    }

    func collectionView(_ collectionView: UICollectionView, layout
        collectionViewLayout: UICollectionViewLayout,
                        minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 1.0
    }
然后实现UICollectionViewDelegateFlowLayout方法。

// MARK: - UICollectionViewDataSource protocol

// tell the collection view how many cells to make
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 3
}


// make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {



    print("indexPath.row \(indexPath.row)")
    if indexPath.row == 0{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "firstCell", for: indexPath)
        if let button = cell.contentView.viewWithTag(2) as? UIButton{
            button.addTarget(self, action: #selector(MoveToNextCell), for: .touchUpInside)
        }
        return cell
    }

    else if indexPath.row == 1{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "secondCell", for: indexPath)
        if let button = cell.contentView.viewWithTag(3) as? UIButton{
            button.addTarget(self, action: #selector(MoveToNextCell), for: .touchUpInside)
        }

        return cell
    }

    else{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "thirdCell", for: indexPath)
        if let button = cell.contentView.viewWithTag(4) as? UIButton{
            button.addTarget(self, action: #selector(MoveToNextCell), for: .touchUpInside)
        }
        return cell
    }
}


// MARK: - UICollectionViewDelegate protocol

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    // handle tap events
    print("You selected cell #\(indexPath.item)!")
}
func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {

            return CGSize(width: self.view.frame.width , height: self.view.frame.height)


    }

    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 1.0
    }

    func collectionView(_ collectionView: UICollectionView, layout
        collectionViewLayout: UICollectionViewLayout,
                        minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 1.0
    }
然后在单元格之间移动

func MoveToNextCell(){

        let collectionBounds = self.mainCollectionView.bounds
        let contentOffset = CGFloat(floor(self.mainCollectionView.contentOffset.x + collectionBounds.size.width))
        self.moveToCell(contentOffset: contentOffset)
    }

    func moveToPreviousCell(){
        let collectionBounds = self.mainCollectionView.bounds
        let contentOffset = CGFloat(floor(self.mainCollectionView.contentOffset.x - collectionBounds.size.width))
        self.moveToCell(contentOffset: contentOffset)
    }

    func moveToCell(contentOffset: CGFloat){
        let frame: CGRect = CGRect(x : contentOffset ,y : self.mainCollectionView.contentOffset.y ,width : self.mainCollectionView.frame.width,height : self.mainCollectionView.frame.height)
        self.mainCollectionView.scrollRectToVisible(frame, animated: true)
    }

这几乎就是实现这一目标所需的全部内容。

您应该发布代码来展示您所做的尝试。PageViewController提供ViewController将有助于我想您的问题是否仍然存在?是,我只是不知道如何在集合视图中加载自定义视图,并根据我的JSON加载我需要的视图。你是否尝试过我在下面发布的解决方案?@Valentinscheldeman我的回答有帮助吗?你试过这个方法吗?如果它确实标记为已回答并关闭问题。