如何将特定图像设置到特定阵列位置?

时间:2018-10-30 04:38:00

标签: ios swift

我正在使用UICollectionView进行水平滚动,在array中有六个图像。我要的是单击索引路径x(1)上的项目时,单击一个数组,其中要在除位置1之外的其余项目(0,2,3,4,5)上设置图像。我们可以在特定位置设置特定图像数组的位置,如果是,那么如何?

对于Android,就像

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])

        } 
    }
}

selectedposition是全局值-1的

2 个答案:

答案 0 :(得分:2)

Android 代码段中,我认为您需要按以下方式更改cellForRowAt

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

}

然后在selectedImageButton中设置didSelectItemAt并重新加载collectionView

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

注意:ReloadData可能不建议使用,您应该寻找一种更具反应性的方式来通知单元格更新图像。

答案 1 :(得分:1)

将此添加到cellForItemAt

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

并跟随到didSelectItemAt

collectionView.reloadData()

但是建议将设置所有这些逻辑的单元移到下面提到的单元格中

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])
        }
    }
}