Mike Wu Mike Wu - 2 months ago 15
Swift Question

How to pass on data from multiple sections in a UICollectionView

I'm new to Swift. I'm trying to show an image in a detail view, and this image comes from a

UICollectionViewController
. My questions are:


  1. How can I send the data to the detail view via segue? I can't use
    if let indexPath = collectionView.indexPathForSelectedRow...
    So what is the right way?

  2. How can I tell when the user taps on images from different sections of the
    UICollectionViewController
    and then pass this information on to the
    DetailView
    ?



AudiUICollectionViewController.swift:

import UIKit

class AudiUICollectionViewController: UICollectionViewController {

var audiPictures01:[carData] = [
carData(name:"01", picture:"1"),
carData(name:"02", picture:"2"),
carData(name:"03", picture:"3"),
carData(name:"04", picture:"4"),
carData(name:"05", picture:"5"),
carData(name:"06", picture:"6")]

var audiPictures02:[carData] = [
carData(name:"07", picture:"7"),
carData(name:"08", picture:"8"),
carData(name:"09", picture:"9"),
carData(name:"10", picture:"10"),
carData(name:"11", picture:"11"),
carData(name:"12", picture:"12")
]


override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 2
}

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
return audiPictures01.count
} else {
return audiPictures02.count
}

}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

if indexPath.section == 0 {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! AudiUICollectionViewCell

cell.collectionPicture.image = UIImage(named: audiPictures01[indexPath.row].picture)
return cell
} else {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! AudiUICollectionViewCell

cell.collectionPicture.image = UIImage(named: audiPictures02[indexPath.row].picture)
return cell
}


}

//I don't know how to set the segue to pass the data

}


AudiDetailViewController.swift:

import UIKit

class audiDetailViewController: UIViewController {

@IBOutlet weak var audiDetailImageView: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()

}
}

Answer

you can use prepareForSegue() functionality to data between your controller from parents to it's child or another way your detailViewController, I add the prepareForSegue() to your code and it should be look like this

 import UIKit

 class AudiUICollectionViewController: UICollectionViewController {

 var audiPictures01:[carData] = [
                          carData(name:"01", picture:"1"),
                          carData(name:"02", picture:"2"),
                          carData(name:"03", picture:"3"),
                          carData(name:"04", picture:"4"),
                          carData(name:"05", picture:"5"),
                          carData(name:"06", picture:"6")]

 var audiPictures02:[carData] = [
                          carData(name:"07", picture:"7"),
                          carData(name:"08", picture:"8"),
                          carData(name:"09", picture:"9"),
                          carData(name:"10", picture:"10"),
                          carData(name:"11", picture:"11"),
                          carData(name:"12", picture:"12")
                          ]


override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 2
}

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
    return audiPictures01.count
} else {
    return audiPictures02.count
}

}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

if indexPath.section == 0 {
    let cell =          collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! AudiUICollectionViewCell

    cell.collectionPicture.image = UIImage(named: audiPictures01[indexPath.row].picture)
    return cell
} else {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! AudiUICollectionViewCell

    cell.collectionPicture.image = UIImage(named:       audiPictures02[indexPath.row].picture)
    return cell
}


}

//I don't know how to set the segue to pass the data
// use this line of code in your view controller

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    if let detailVC = segue.destinationViewController as? audiDetailViewController {
        if segue.identifier == "showDetailViewController" {
            let selectedRow = self.collectionView!.indexPathForCell((sender as! UICollectionViewCell))
            var category: carData?
            if selectedRow!.section == 0 {
                category = audiPictures01[selectedRow!.row]
            } else {
                category = audiPictures02[selectedRow!.row]
            }
            detailVC.name = category.name
            detailVC.picture = category.picture
        }
    }

}

}

and in DetailViewController you should create a var for name and picture or you can just make one variable of your own object to pass data to it. I just create 2 var one for 'name' and the other for 'picture' and you detailController looks like this

import UIKit

class audiDetailViewController: UIViewController {

@IBOutlet weak var audiDetailImageView: UIImageView!
var picture: String?
var name: String?

override func viewDidLoad() {
    super.viewDidLoad()
         print(name, picture)
    }
}

hope this will help