2 Collection View in the same ViewController?

2

I enclose an image of what I want to do:

In the following code, I have a code from a collection where when I click on an image I move with one segue to another VC with the same larger image. How could I change the segue to put the big image in the same VC?

This is my current code:

import UIKit

class CollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    @IBOutlet weak var collectionView: UICollectionView!

    let tvSeries = ["Perdidos", "Friends", "Breaking Bad", "Dexter"]

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let identifier = "Item"

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as! SeriesCollectionViewCell

        cell.itemLabel.text = tvSeries[indexPath.row]
        cell.itemImage.image = UIImage.init(imageLiteralResourceName: tvSeries[indexPath.row])

        return cell
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let item = sender as? UICollectionViewCell
        let indexPath = collectionView.indexPath(for: item!)
        let detailVC = segue.destination as! DetailViewController
        detailVC.detailName = tvSeries[(indexPath?.row)!]
    }


}
    
asked by Dani 08.06.2017 в 10:22
source

2 answers

0

To have 2 collectionViews in the same controller you do not have to do any segue. What you have to have are 2 collectionViews (imagine collectionViewUp and collectionViewdown) and implement the method didSelectItemAtIndexPath, once you have clicked on a cell in collectionView, you can access the element clicked in the same way you have, using tvSeries [indexPath.row]

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    if collectionview == collectionViewAbajo {
        let item = tvSeries[indexPath.row]

        //imagino que de aqui podras sacar la imagen y se la puedes poner a la imagen que deberias tener encima del collcetionView
        self.imagen.image = item.imagen
    }
}

I am not very clear about the 2 collectionView. If what you want is that when you click on an image below it appears in another collectionView above, that's another thing, you should have the images in the collectionView above already loaded, or when you click on the one below that appears in the above or a method to know that when you click on the one below it should be reflected in the one above. You could be more specific, but imagining that you have another array as a data model only with the collectionView images. Below you could do this:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    if collectionView == collectionViewAbajo {
        collectionViewArriba.scrollToItemAtIndexPath(indexPath, atScrollPosition: .CenteredHorizontally, animated: true)
    }
}

With collectionViewUp the large collectionView, in which the data model is related to tvSeries, so that element 1 is revered by element 1 of that other data model.

Sorry I can not help more, but it has not been clear to me how you want to show the images above or how you have implemented it.

    
answered by 19.07.2017 в 18:49
0

It does not seem bad how you are raising it ... I think that it is still necessary to register the class of the cells and add them to the id of the cells in your case it is "item" ... you have already created the items in the sections ... and also the cells .. certainly no need to segue ...

the first thing is create a class: // if you already have it is obviously not necessary!

class CellTvSeries: UICollectionViewCell {      override init (frame: CGRect)           super.init (frame: frame)

// en este punto es donde se colocan los objetos que vas creando es como el viewDidLoad()

} (here the compiler needs a required init, you simply click on the automatic fix button and Xcode places it for you ...

your collections where you have your tv series simply implement them in this class ...

and to register the cells ...

in the viewDidLoad () ... collectionView.register ("created class", forcellWithReusIdentifier: "id of your cells"

also something else .. you need to give size to those cells .. add in the class UICollectionViewDelegateFlowLayout so you can use the function

sizeForItemAt this returns a CGRect .. you put:

return CGRect (width: view.frame.widht, height: 140 "or the size you want")

and with that you already have to be able to see your collectionView ...

This way is without using the storyboard, but it can serve you ... and with that, the collection must already appear in your ViewController, do not forget to give it the properties you need such as the scroll and other greetings I hope it helps you

    
answered by 31.08.2017 в 07:22