2017-02-17 2 views
0

私はを持っています。これは、そのセルにカスタムxibファイルを使用しています。セルでは、チェックボックスにImageViewがあります。私はImageView1の画像をテープで録画したときに変更したい。私はそれが私がdidSelectItemAtImageViewの画像を変更しようとしました。このコードでは、私タップでUICollectionViewCellのImageViewイメージを変更します。

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 
} 

のために働いていない、次のコードスニペットを試みたが。

私のカスタムxibファイルは次のとおりです。

enter image description here

それを動作させるための方法を提案してください。ありがとう。

+0

解決策を試したことがありますか? – KrishnaCA

+0

@KrishnaCAあなたの提案を少し修正して解決しました。リードしてくれてありがとう。 –

+0

これは私のソリューションに似ています。しかし、完全なブール値配列の代わりにint配列を使用して、メモリの足の印刷を減らしています。ニース – KrishnaCA

答えて

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 
} 
0

最後に、次のコードで解決しました。

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にアイテムを再読み込みしました。配列にリロードされたインデックスが含まれている場合はチェックマークが表示され、チェックマークが削除されない場合はチェックマークが表示されます。

関連する問題