How to index image from xcode storyboard to collectionview

Added imageview to cell. We have already indexed the label and implemented openurl. But, I don't know how to index the image the way I wrote it. Currently, no error code is entered, but the picture is not visible in the simulator.

import UIKit

private let reuseIdentifier = "Cell"

class CollectionViewController: UICollectionViewController {   var modelData = ["A","B","B","D","E"]       let urls = ["h","h","h","h","h",]       let imagedata = [UIImage(named: "image1"), UIImage(named: "image2"), UIImage(named: "image3"), UIImage(named: "image4"), UIImage(named: "image5")]        

          @IBAction func URLbutton(_ sender: UIButton) {         }           override func viewDidLoad() {     super.viewDidLoad()                     }

  // MARK: UICollectionViewDataSource

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

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

  override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)           // Configure the cell     if let label = cell.viewWithTag(100) as? UILabel {       label.text = modelData[indexPath.row]     }     if let imageview = cell.viewWithTag(200) as? UIImageView {       imageview.image = imagedata[indexPath.row]     }                                                   return cell   }   override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {     ??     }

When you paste code, please use code formatter to make it readable:

import UIKit

private let reuseIdentifier = "Cell"

class CollectionViewController: UICollectionViewController {
    var modelData = ["A","B","B","D","E"]
    let urls = ["h","h","h","h","h",]
    let imagedata = [UIImage(named: "image1"), UIImage(named: "image2"), UIImage(named: "image3"), UIImage(named: "image4"), UIImage(named: "image5")]
    
    @IBAction func URLbutton(_ sender: UIButton) {
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
  // MARK: UICollectionViewDataSource
    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return modelData.count
    }
    
  override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
      // Configure the cell
      if let label = cell.viewWithTag(100) as? UILabel {
          label.text = modelData[indexPath.row]
      }
      if let imageview = cell.viewWithTag(200) as? UIImageView {
          imageview.image = imagedata[indexPath.row]
      }
      return cell
  }
    
    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        ??
    }

}

I don't understand your question ?

  • Is it how to write didSelectItemAt(in place of ??) ?

What do you want to do when selecting cell ?

  • Is it that the picture is not visible in the simulator ?
    • does the label show correctly ?
    • Have you included "image1"… in the project resources ? How ?
    • how did you define the cell in storyboard ?

.

how to index the image

What do you mean ?

My question is in the title. T I T L E.For security reasons image1, 2, 3 have been changed

Your question may be in the title, but it is not C L E A R.

If you don't care answering the questions (asked to try to help you), you will probably better solve the problem by yourself.

Wish you good.