ios - 如何将特定图像设置到特定数组位置?

标签 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

最佳答案

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

}

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

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

注意:ReloadData 可能不被推荐,您应该寻找一些更具 react 性的方式来通知单元格更新图像。

关于ios - 如何将特定图像设置到特定数组位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53057525/

相关文章:

arrays - 根据搜索文本过滤核心数据项

ios - 按钮应该在点击后改变图像,但(随机)也改变其他按钮的图像

ios - 我的非ATS支持应用程序如何在iOS9上运行?

swift - Swift 中的关键字 repeating 有什么用?

ios - 定时器未同步

Swift 核心数据 NSPredicate

ios - 当我模拟后台提取时,我收到一条警告,说从未调用完成处理程序

ios - 我应该如何正确地向 AVCaptureVideoPreviewLayer 添加约束?

ios - Swift Parse - 嵌套调用问题

ios - 应用程序内的钥匙串(keychain)解锁,这种方法安全吗?