1

I am trying to pass data (selectedItem) of collectionView to imageViewController. I am not getting the counter incremented to swipe up the next image. When I click the cell, I get the first image(selected image) correctly. But when I swipe, the image starts the first element of the array(imageViewController). Where am i going wrong?

SelectedItem is the value I am passing from my collectionViewController to the imageViewController.

 import UIKit

    class ImageViewController: UIViewController,UIScrollViewDelegate {

    @IBOutlet weak var pageControl: UIPageControl!
    @IBOutlet weak var myScrollView: UIScrollView!
    var myImage = UIImage()
    var selectedItem: String!

    var images: [String] = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"]
    let imageView = UIImageView()
    var frame = CGRect(x:0,y:0,width:0,height:0)

override func viewDidLoad() {

    super.viewDidLoad()
    setupImageView()
    }
private func setupImageView() {

    var selectedImage = (selectedItem as NSString).integerValue


     for var index in 0..<images.count {


        frame.origin.x = myScrollView.frame.size.width * CGFloat(index)
                     frame.size = myScrollView.frame.size
                     let imageName = UIImageView(frame:frame)
                     imageName.image = myImage
                     index = selectedImage
                   myImage = UIImage(named: images[index])!
                   print(index)
                     myScrollView.showsVerticalScrollIndicator = false
                     self.myScrollView.addSubview(imageName)

}

    myScrollView.contentSize = CGSize(width: (myScrollView.frame.size.width * CGFloat(images.count)), height: myScrollView.frame.size.height)
     myScrollView.delegate = self

        }
        func scrollViewDidScroll(_ scrollView: UIScrollView) {
            let pageIndex = round(scrollView.contentOffset.x/view.frame.width)
            pageControl.currentPage = Int(pageIndex)
        }

    }
Nikunj Kumbhani
  • 3,758
  • 2
  • 26
  • 51
NelsonDharu
  • 63
  • 1
  • 7

2 Answers2

0

To work with a selected item, you should override the collectionView didSelectItemAt function:

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    pageControl.currentPage = indexPath.row
}

Also, your code is very different from your question and I don't quite understand your overall goal. If you want to create a scrolling list of images, you should subclass UICollectionViewCell instead of adding UIImageView's to a UIScrollView. You can see an example GalleryViewController in this open-source snippet of mine.

elliott-io
  • 1,394
  • 6
  • 9
0
  private func setupImageView() {
        for var index in 0..<images.count {

        frame.origin.x = myScrollView.frame.size.width * CGFloat(index)
        frame.size = myScrollView.frame.size
        let imageName = UIImageView(frame:frame)
        imageName.image = UIImage(named: images[index])!
        print(index)
        myScrollView.showsVerticalScrollIndicator = false
        self.myScrollView.addSubview(imageName)

    }

  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        pageControl.currentPage = indexPath.row
    }

    myScrollView.contentSize = CGSize(width: (myScrollView.frame.size.width * CGFloat(images.count)), height: myScrollView.frame.size.height)
    myScrollView.delegate = self


    let indexpaths = UserDefaults.standard.value(forKey: "IndexPath") as! Int
    let x = Double(414 * indexpaths)  // here 414 is your current scroll view frame size 
    myScrollView.contentOffset = CGPoint(x: x, y: 0.0)
  }

just change your code with this code and your problem is sloved.
Thank you
Vipin Pareek
  • 236
  • 2
  • 10