CollectionView Cell clicked expand new view

367 views Asked by At

I'm trying to create a demo app that has a TabBarViewController as well as 4 tabs ( 4 Different ViewControllers on the storyboard)

On the first Tab which is the first ViewController, I have a collectionView with Cells that have ImageView and few Labels.

What I'm trying to do is, when you click on a Cell, It expands to the whole screen, and the view still stays in the first Tab from the Tab Bar, just now that the view inside the ViewController has changed to a new view that is an expanded view of the Cell that was selected, and when you scroll down the collection view continues, and you can click another cell and it expands and so on...while you can at any time press back and it shows the last cell that was expanded in the view

I hope I was clear enough about what I'm trying to do. now ill add the code of what I've got already

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

There are 1 answers

1
Bhavuk Chawla On

You have reload the collection view and also call size delegate of collection view