2017-02-17 119 views
0

我有一個UICollectionView,它使用定製的xib文件作爲它的單元格。在單元格中,我有一個ImageView複選框。我想在錄製時更改ImageView1的圖像。我試着下面的代碼片段,但是,它不適合我在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 
} 

工作在這個代碼中,我試圖改變的ImageView圖像中didSelectItemAt

我的自定義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的項目。如果數組包含重新加載的索引,則它將顯示覆選標記,如果不包含,則刪除複選標記。