【问题标题】:Change ImageView image on UICollectionViewCell on Tap点击时更改 UICollectionViewCell 上的 ImageView 图像
【发布时间】:2017-02-17 07:28:34
【问题描述】:

我有一个UICollectionView,它使用自定义xib 文件作为它的单元格。在单元格中,我有一个 ImageView 复选框。我想在录制时更改`ImageView1 的图像。我尝试了以下代码 sn-p 但它不适合我

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! V_Cell

    if show_delete == true {
        cell.img_delete.image = UIImage(named: "checked")
    }
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! V_Cell

    // Configure the cell

    let data = valves_[indexPath.row]
    cell.v_name.text = data

    if show_delete == true {
        cell.img_delete.isHidden = false
    } else if show_delete == false {
        cell.img_delete.isHidden = true
    }

    return cell
}

在这段代码中,我尝试在didSelectItemAt 中更改ImageView 的图像。

我的自定义xib文件如下。

请提出一种使它起作用的方法。谢谢。

【问题讨论】:

  • 请问您是否尝试过我的解决方案?
  • @KrishnaCA 我通过稍微调整您的建议解决了问题。感谢您的领导。
  • 这与我的解决方案几乎相似。但是,您没有使用完整的布尔数组,而是使用 int 数组来减少内存占用。不错

标签: ios swift swift3 uicollectionview uicollectionviewcell


【解决方案1】:

对于这类问题,最好保留一个数组,其中包含是否检查给定indexpath 处的项目。

var checkArray: [Bool] = [Bool](repeating: false, count: numberOfRowsInCollectionView)
// this should be the same size as number of items in collection view

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! V_Cell
    checkArray[indexPath.row] = !checkArray[indexPath.row] // if it's true, make it false and vice versa logic
    collectionView.reloadItems(at: [indexPath])
}

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! V_Cell

    // Configure the cell

    let data = valves_[indexPath.row]
    cell.v_name.text = data

    if show_delete == true {
        cell.img_delete.isHidden = false
    } else if show_delete == false {
        cell.img_delete.isHidden = true
    }

    if checkArray[indexPath.row] {
        cell.img_delete.image = //image for check
    }else {
        cell.img_delete.image = //image for no check
    }

    return cell
}

【讨论】:

    【解决方案2】:

    最后我通过下面的代码解决了。

        var checkArray = [Int]()
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if show_delete == true {
            if checkArray.contains(indexPath.row) {
                let index = checkArray.index(of: indexPath.row)
                checkArray.remove(at: index!)
                collectionView.reloadItems(at: [indexPath])
            } else {
                checkArray.append(indexPath.row)
                collectionView.reloadItems(at: [indexPath])
            }
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! V_Cell
    
        // Configure the cell
        let data = valves_[indexPath.row]
        cell.v_name.text = data
    
        if show_delete == true {
            cell.img_delete.isHidden = false
        } else if show_delete == false {
            cell.img_delete.isHidden = true
        }
    
        if checkArray.contains(indexPath.row) {
            cell.img_delete.image = UIImage(named: "checked_n")
        } else {
            cell.img_delete.image = UIImage(named: "unchecked")
        }
    
        return cell
    }
    

    我将记录的元素索引放入一个数组中,并在 indexpath 处重新加载项目。如果数组包含重新加载的索引,则显示复选标记,否则删除复选标记。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 2018-06-16
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多