在点击

时间:2017-02-17 07:28:34

标签: ios swift swift3 uicollectionview uicollectionviewcell

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

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
}

在此代码中,我尝试更改ImageViewdidSelectItemAt的图像。

我的自定义xib文件如下。

enter image description here

请建议一种让它运作的方法。感谢。

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

最后我通过以下代码解决了它。

INSERT INTO my_table (other_column, third_column) VALUES (…, …);
    -- not specifying any value for my_column manually,
    -- it'll be initialized automatically to some value
    -- (usually an increasing integer sequence)

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