acidosulfurico50
acidosulfurico50

Reputation: 27

Passing an array of images from tableview to collectionview

I have to view controllers. First, a table view and second a collection view in a second detail view. I created an array of arrays of images. I expect to click in the cell table and pass one array of images to the collection view. I defined a file model with the array of arrays. I defined two more controllers for cell table and cell collection views as well.

Problem: I can not pass this array of array images. I did pass an array of strings to a textview in the detail using prepare for segue. Volk there, I will really appreciate your help. Can take a look to the project in GitHub: https://github.com/ricardovaldes/hotelesWithIos9

At the moment, when I click the table cell I obtain some pictures of different arrays, and no matter which cell I click, always the same pictures.

import UIKit

class DetailVCViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    let hotel = Hotel()
    var myDescription = ""

    @IBOutlet weak var myCollection: UICollectionView!

    @IBOutlet weak var label: UILabel!

    @IBOutlet weak var hotelDescription: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        hotelDescription.text = myDescription
    }


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

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

        let cell = myCollection.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! CollectionCellCollectionViewCell

        cell.cellImage.image = UIImage(named: hotel.array[indexPath.row][indexPath.row])

        return cell
    }

    @IBAction func reservar(_ sender: UIButton) {

    }


}


import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var myTable: UITableView!

    let hotel = Hotel()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let cell = UINib(nibName: "TableViewCell", bundle: nil)
        myTable.register(cell, forCellReuseIdentifier: "tableCell")

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return hotel.hotelNames.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = myTable.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! TableViewCell

        cell.hotelName.text = hotel.hotelNames[indexPath.row]
        cell.hotelImage.image = UIImage(named: hotel.hotelImages[indexPath.row])
        cell.hotelImage.contentMode = .scaleToFill
        return cell

    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 250
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "mySegue", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let index = myTable.indexPathForSelectedRow
        let indexNumber = index?.row
        let secondVC = segue.destination as! DetailVCViewController
        secondVC.myDescription = hotel.description[indexNumber!]

    }

}

Upvotes: 0

Views: 1200

Answers (1)

Kamran
Kamran

Reputation: 15238

In DetailVCViewController, you are creating a new object of Hotel, so this object is not the same object that you selected in the TableView cell in ViewController. Replace hotel object to an array of String as below so that you can pass the array from the selected TabelView cell index to DetailVCViewController

class DetailVCViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    var array: [String] = []
    var myDescription = ""
    ....
} 

Now in ViewController, update the prepare(for segue: as below to pass the array

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

   ....

   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      let index = myTable.indexPathForSelectedRow
      let indexNumber = index?.row
      let secondVC = segue.destination as! DetailVCViewController 
      secondVC.myDescription = hotel.description[indexNumber!]
      secondVC.array = hotel.array[indexNumber!]

   }
}

Upvotes: 1

Related Questions