Setting Images and Name in Collection View swift 5

Viewed 418

So im trying to set images and their name in a collection view what ive achieved is that ive set the names but im unable to set the images to that collection view

im setting up the array of names and icons here

let dataArray = ["Home", "Appointment", "Teleconsultation", "Lab", "Pharmacy", "Registration", "Careers", "Utilities","Feedback"]
    var icons : [UIImage] = [UIImage(named: "appointment.png")!, UIImage(named: "telehealth.png")!, UIImage(named: "lab.png")!, UIImage(named: "pharmacy.png")!, UIImage(named: "labreports.png")!, UIImage(named: "laborder.png")!, UIImage(named: "pharmacyorder.png")!, UIImage(named: "logout.png")!]

and then im trying to load that up to my collection view using this

im getting the names in the collection view but not the image func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return dataArray.count }

          func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
              let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ItemCell", for: indexPath)  as! ItemCell
              cell.setData(text: self.dataArray[indexPath.row])
              cell.setData(UIImageView: self.dataArray[indexPath.row])


              return cell
          }

This line gives error when i use it that says : Incorrect argument label in call (have 'UIImageView:', expected 'text:')

          cell.setData(UIImageView: self.dataArray[indexPath.row])

my ItemCell Class

class ItemCell: UICollectionViewCell {

    @IBOutlet weak var textLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()

        self.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
        self.layer.shadowOffset = CGSize(width: 0.0, height: 3.0)
        self.layer.shadowOpacity = 1.0
        self.layer.shadowRadius = 10.0
        self.layer.masksToBounds = false
        self.contentView.layer.cornerRadius = 16.0
            }

    func setData(text: String) {
        self.textLabel.text = text



    }


}
1 Answers

Here the error states clearly that you don't have a method setData(UIImageView: in your ItemCell class. Add one to the ItemCell class that accepts type UIImage and implement the method to set the image to the imageView and you're good to go. Before that make sure the cell has an UIImageView added and it's connected to the @IBOutlet in the ItemCell for imageView. Here's an example:

class ItemCell: UICollectionViewCell {
    @IBOutlet weak var imageView: UIImageView!

    func setData(image: UIImage) {
        imageView.image = image
    }
}

Note: You should modify your naming conventions to some extend too.

Related