单击CollectionView单元格展开新视图

时间:2016-12-30 15:09:05

标签: swift uicollectionviewcell uicollectionviewdelegate

我正在尝试创建一个具有TabBarViewController以及4个标签的演示应用程序(故事板上有4个不同的ViewControllers)

在作为第一个ViewController的第一个Tab上,我有一个collectionView 使用具有ImageView和少量标签的单元格。

我要做的是,当你点击一个Cell时,它会扩展到整个屏幕,并且视图仍然保留在Tab Bar的第一个Tab中, 刚刚ViewController中的视图已更改为新视图,该视图是所选单元格的展开视图,当您向下滚动集合视图时,您可以单击另一个单元格并展开,依此类推。 。你可以随时按回来,它会显示在视图中展开的最后一个单元格

我希望我对自己要做的事情已经足够清楚了。现在我添加了我已经拥有的代码

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {


    var imagesArray = []

    var imageSizes = [CGSize]()

    var labelsArray = [""]

    var descArray = [""]



    @IBOutlet weak var collectionView: UICollectionView!


    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView.delegate = self
        self.collectionView.dataSource = self

        for i in 0..<imagesArray.count {

            let currImg = imagesArray[i]
            let currHeight = currImg.size.height
            let currSize = CGSize(width: 150, height: currHeight + 125)
            imageSizes.append(currSize)
        }



       }





    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imagesArray.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionVCe

         cell.cellImg.image = imagesArray[indexPath.row]
        cell.descLabel.text = descArray[1]
        cell.itemLabel.text = labelsArray[0]


            cell.cellImg.contentMode = .scaleAspectFill
        cell.layer.cornerRadius = 9

      cell.backgroundColor = UIColor.red
        cell.cellImg.backgroundColor = UIColor.blue




        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return imageSizes[indexPath.row]
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(0, 25, 0, 25)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10

    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let cell = collectionView.cellForItem(at: indexPath) {
            cell.frame = collectionView.frame

        }
    }



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
        }


}

1 个答案:

答案 0 :(得分:0)

您已重新加载集合视图,并且还调用集合视图的大小委托