Passing information from a collection view to a tableview

Viewed 77

Right now I have a table view that is an extension of a collection view.

Here is a picture of the storyboard. enter image description here

The main premise of the app is similar to Apple Maps. I would like for a collectionView cell to be selected and then for the viewController to display a tableView that has all of the respected items from that category.

Here is my code for the collectionView.

 class ContentViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

let imageArray = [UIImage(named: "image 1"), UIImage(named: "image 2"), UIImage(named: "image 3")]

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell
    cell.mapIconImage.image = imageArray[indexPath.row]
    cell.mapIconLabel.text! = imageNameArray[indexPath.row]
    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    performSegue(withIdentifier: "ShowTableViewPlaces", sender: self)
}

class CustomCell: UICollectionViewCell {

@IBOutlet weak var mapIconImage: UIImageView!
@IBOutlet weak var mapIconLabel: UILabel!

}

Here is the code so far for the tableView.

import UIKit

struct PlacesInTableView {
var name: String

init(name: String) {
    self.name = name
}
}

class MapItemsTableViewController: UITableViewController {

var image1InTableView = [PlacesInTableView(name: "place 1"),
    PlacesInTableView(name: "place 2"),
    PlacesInTableView(name: "place 3")
]

var image2InTableView = [PlacesInTableView(name: "place 1")
]

var image3InTableView = [PlacesInTableView(name: "place 1"),
    PlacesInTableView(name: "place 2")
]

When a collection view is selected, I would like all of the locations in that respected category to occupy the contents of the tableview. How would I go about passing data from the collection view cell to a new tableView?

2 Answers

As I understand, you wanna pass the name value to the MapItemsTableViewController ?

You can override prepare function :

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let viewController = segue.destination as! MapItemsTableViewController

    viewController.name = // Data can input here

}

add storyboard id for table vc "MapItemsTableViewController" and add below code in ContentViewController

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let vc = self.storyboard?.instantiateViewController(withIdentifier: "MapItemsTableViewController") as? MapItemsTableViewController {
            vc.imageCate = indexPath.row
            self.present(vc, animated: true, completion: nil)
        }
}

and then you can implement as below for MapItemsTableViewController

class MapItemsTableViewController: UITableViewController {
    var imageCate:Int?
    var image1InTableView = [PlacesInTableView(name: "place 1"),
                             PlacesInTableView(name: "place 2"),
                             PlacesInTableView(name: "place 3")
    ]

    var image2InTableView = [PlacesInTableView(name: "place 1")
    ]

    var image3InTableView = [PlacesInTableView(name: "place 1"),
                             PlacesInTableView(name: "place 2")
    ]

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if imageCate == 0 {
           return image1InTableView.count
        }
        else if imageCate == 1{
            return image2InTableView.count
        }else if imageCate == 2{
            return image3InTableView.count
        }else{
            return 0
        }
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()

        if imageCate == 0 {
            cell.textLabel?.text = image1InTableView[indexPath.row].name
        }
        else if imageCate == 1{
            cell.textLabel?.text = image2InTableView[indexPath.row].name
        }else if imageCate == 2{
            cell.textLabel?.text = image3InTableView[indexPath.row].name
        }else{

        }
        return cell
    }

}
Related