I'am using a UICollectionView to download and display Images from Backendless Database , How can I display these Images again in a ViewController that contain ImageView when I tap on any row of My CollectionView using Swift3 .

This is My CollectionView.

import UIKit
import SDWebImage

class GallaryCollectionViewController: UICollectionViewController {
    var GallaryArray = [TestTable]()
    let backendless = Backendless()

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        loaddatawithquery()
    }

    // MARK: UICollectionViewDataSource

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

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

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

        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? GallaryCell {

            let ImgURL = URL(string : self.GallaryArray[(indexPath as NSIndexPath).row].ImgURL!)
            cell.GallImag.sd_setImage(with: ImgURL)

            cell.ShowImg = {
                if  let myimage = (cell.GallImag.image) {
                    // Error here How to complete the code ?
                }

            }
            return cell
        }
        else {
            let cell = GallaryCell()
            let ImgURL = URL(string : self.GallaryArray [(indexPath as NSIndexPath).row].ImgURL)
            cell.GallImag.sd_setImage(with: ImgURL)
            cell.ShowImg = {
                if  let myimage = (cell.GallImag.image) {

                } 
            }
            return cell
        }
    }

    func loaddatawithquery() {        
        let dataQuery = BackendlessDataQuery()
        dataQuery.queryOptions.pageSize = 50

        backendless.data.of(TestTable.ofClass()).find(dataQuery,response: {( result: BackendlessCollection?) -> Void in

            let data = result?.getCurrentPage()

            for obj in data! as! [TestTable] {
                self.GallaryArray.append(obj)
                self.collectionView?.reloadData()
            }

        }, error: { (fault: Fault?) -> Void in
            let alert = UIAlertController(title: "انتباه", message:"يرجى الاتصال بالانترنيت", preferredStyle: .alert)

            alert.addAction(UIAlertAction(title: "OK", style: .default) { _ in })
            self.present(alert, animated: true){}
        })
    }

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {}
}

And this is My Cell.

  import UIKit

  class GallaryCell: UICollectionViewCell  {

@IBOutlet weak var GallImag: UIImageView!

var ShowImg : (() ->Void)? 


    func setSelected(_ selected: Bool, animated: Bool) {
    setSelected(selected, animated: animated)

    self.ShowImg!()
  }

}
1

There are 1 best solutions below

4
On

I'm going to assume you have your images loading correctly in the collection view.

there is a delegate method you can use called didSelectRow...

func collectionView(_ collectionView: UICollectionView, didSelectRowAt indexPath: IndexPath) {

    self.passingTestTable = self.GallaryArray[indexPath.row]

    self.performSegue(withIdentifier: "segue for new view controller", sender: nil)


}

self.passingTestTable is another property you create globally at the top of TestTable() (this is how I do it, some may choose to create the instance inside the didSelectRow

the other thing you'll need to do is create an instance of TestTable on the receiving viewController (i.e self.receivedTestTable )

in prepare for segue you can select the item selected and send it over to the new viewController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "yourSegueName" {

        if let controller = segue.destination as? PassedImageDetailViewController {

            controller.receivedTestTable = self.passingTestTable
        }

        else {
            print("Your segue identifier is incorrect")
        }
    }
}

once you've done that, the detail View Controller has access to the image data and you can display in in an image view on that screen.