Nishant Pathania
Nishant Pathania

Reputation: 349

How to Set a specific Image to a specific array position?

I am using a UICollectionView for Horizontal scrolling having six images in the array. What I want is when an item at index path x(1) is clicked an array with images to be set on remaining items(0,2,3,4,5) except position 1. Can we set a specific image at a specific position of an array if yes then how?

In the case of Android, it is like

if (selectedPosition < 0) {
  viewHolder.imageView.setImageResource(coloredSmiley[position]);
} else {
  viewHolder.imageView.setImageResource(selectedPosition == position ? coloredSmiley[position] : greySmiley[position]);
}


import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    var selectedImageButton = -1
    var imagearray = ["AngrysmIcon1", "UpsetsmIcon2", "ConfusedsmIcon3", "MehsmIcon4", "CurioussmIcon5" , "HappysmIcon6"]
    var bwimagearray = ["AngrysmIcon1Inactive", "UpsetsmIcon2Inactive", "ConfusedsmIcon3Inactive", "MehsmIcon4Inactive", "CurioussmIcon5Inactive", "HappysmIcon6Inactive"]

    var positiontag = ["0", "1", "2", "3", "4", "5"]

    override func viewDidLoad() {
        super.viewDidLoad()

        }

  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

            return self.positiontag.count
    }

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

            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! UIcollectionViewCellCollectionViewCell
            cell.imagev.image = UIImage(named: imagearray[indexPath.row])
            return cell

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

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

        selectedImageButton = indexPath.row

        let cell = collectionView.cellForItem(at: indexPath) as! UIcollectionViewCellCollectionViewCell
        if selectedImageButton < 0 {

            //cell.imagev.image = bwimagearray[indexPath.row]

        } else {
            cell.imagev.image = UIImage(named: imagearray[indexPath.row])

        } 
    }
}

Where selectedposition is global with value -1

Upvotes: 3

Views: 251

Answers (2)

Kamran
Kamran

Reputation: 15238

From the Android snippet, i believe you need to change cellForRowAt as below,

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! UIcollectionViewCellCollectionViewCell
      let imageName: String
      if selectedImageButton < 0 {
          imageName = imagearray[indexPath.row]
      } else {
          imageName = selectedImageButton == indexPath.row ? imagearray[indexPath.row] : bwimagearray[indexPath.row]
      }
      cell.imagev.image = UIImage(named: imageName)
      return cell

}

And then set the selectedImageButton in didSelectItemAt and reload the collectionView,

internal func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)   {
        selectedImageButton = indexPath.row
        collectionView.reloadData()
}

Note: ReloadData may not be recommended and you should look for some more reactive way to notify the cells to update image.

Upvotes: 2

Satish
Satish

Reputation: 2043

Add this to cellForItemAt

if cell.isSelected {
    cell.imagev.image = UIImage(named: imagearray[indexPath.row])
} else {
    cell.imagev.image = UIImage(named: bwimagearray[indexPath.row])
}

and following to didSelectItemAt

collectionView.reloadData()

But would recommend to move the logic that set all this to cell something like below mentioned

class UIcollectionViewCellCollectionViewCell: UICollectionViewCell {
    override var isSelected: Bool {
        get {
            return super.isSelected
        }
        set {
            super.isSelected = newValue
            updateImage()
        }
    }

    private func updateImage() {
        if isSelected {
            imagev.image = UIImage(named: imagearray[indexPath.row])
        } else {
            imagev.image = UIImage(named: bwimagearray[indexPath.row])
        }
    }
}

Upvotes: 1

Related Questions