具有多个不同集合viewcell

时间:2018-02-02 21:32:28

标签: ios swift xcode uicollectionview

我正在构建一个调查应用,我想使用这样的观点(the view)

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

我会收到一个JSON来构建它,问题类型和答案可能等等。

我的主要问题是我不知道如何继续,我是iOS开发者的新手,我不想尝试太多东西并且有意大利面条代码所以如果有人了解如何使用不同的视图执行此集合视图,具体取决于json

谢谢你/

1 个答案:

答案 0 :(得分:1)

There is lot of ways to achieve it but as in your case you want with Collectionview with multiple different collection viewcell

In ViewController.swift, conform few UICollectionView protocol.

    class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate , UICollectionViewDelegateFlowLayout{

        @IBOutlet weak var mainCollectionView: UICollectionView!

        override func viewDidLoad() {
            super.viewDidLoad()
             mainCollectionView.isScrollEnabled = false
        }

then implement collection view data source and delegate methods.

// 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)!")
}

then implement UICollectionViewDelegateFlowLayout methods.

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
    }

Then finally for moving between cells

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)
    }

That's pretty much all you need to achieve that.