2017-03-25 13 views
-1

セルをタップして画像を拡大し、拡大した画像をタップすると各セルで閉じることができます。選択可能なチェックマークが付いています後で選択した画像をダウンロードしてください。コードはほとんどの場合、チェックマークを2回選択または選択解除する必要があることを除いてほとんど機能します。私は何が失われたのか本当に分かっていませんが、私のコードはここにあります:コレクションビューのチェックマークを2回タップしてタップしてください

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "photoCell", for: indexPath) as! PhotoCell 
    let tap = UITapGestureRecognizer(target: self, action: #selector(checkmarkWasTapped(_ :))) 

    cell.backgroundColor = .clear 
    cell.imageView.image = UIImage(contentsOfFile: imagesURLArray[indexPath.row].path) 
    cell.checkmarkView.checkMarkStyle = .GrayedOut 
    cell.checkmarkView.tag = indexPath.row 
    cell.checkmarkView.addGestureRecognizer(tap) 

    return cell 
} 

func checkmarkWasTapped(_ sender: UIGestureRecognizer) { 

    let checkmarkView = sender.view as! SSCheckMark 
    let indexPath = IndexPath(row: checkmarkView.tag, section: 0) 

    let imageURL = imagesURLArray[indexPath.row] 

    if checkmarkView.checked == true { 

     checkmarkView.checked = false 
     selectedImagesArray.remove(at: selectedImagesArray.index(of: imageURL)!) 
    } else { 

     checkmarkView.checked = true 
     selectedImagesArray.append(imageURL) 
    } 

    collectionView.reloadItems(at: [indexPath]) 
} 

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

    addZoomedImage(indexPath.row) 
    addGestureToImage() 
    addBackGroundView() 

    view.addSubview(selectedImage) 
} 

助けが素晴らしいでしょう。ありがとうございます

答えて

1

コレクションビューをスクロールすると問題が発生すると思います。コレクションビューではすべてのセルがリロードされ、チェックビューの最新の状態に関する情報はありません。

通常、私はコレクションビューのセルのインデックスを取って変更するチェックマークビューをダブルタップするたびに(コレクションビューの要素数のサイズで、モデルをロードした後に簡単に作成できます) bool配列要素をtrueにします。

var boolArray = [Bool]() 
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "photoCell", for: indexPath) as! PhotoCell 
    let tap = UITapGestureRecognizer(target: self, action: #selector(checkmarkWasTapped(_ :))) 

    cell.backgroundColor = .clear 
    cell.imageView.image = UIImage(contentsOfFile: imagesURLArray[indexPath.row].path) 

     if(boolArray[indexPath.row]){ 
      cell.checkmarkView.checked = false 
     } 
     else{ 
      cell.checkmarkView.checked = true 
     } 

    cell.checkmarkView.checkMarkStyle = .GrayedOut 
    cell.checkmarkView.tag = indexPath.row 
    cell.checkmarkView.addGestureRecognizer(tap) 

    return cell 
} 

醜い解決策かもしれませんが、それはうまくいくと思います。

+0

返信いただきありがとうございます。if文を追加して、各リロード時に選択内容を入れ替えることはありませんか? 'if!(boolArray [indexPath.row]){' – Wazza

+0

boolArray [indexPath.row]がfalseになる前にユーザがブックマークしていないと、セルがロードされたときにcheckmarkViewがチェックされなくなります。それ以外の場合はチェックされます。あなたのcheckmarkViewがそのように働いていたら? –

+0

viewdidloadでbool配列をfalseで埋めて、'if!(boolArray [indexPath.row]) 'を使用すると、それらはすべてチェックされませんが、チェックを選択するとすぐにチェックを外します。 [indexPath.row]) 'それらはすべてチェックされ、それらを選択すると何も起こりません。 – Wazza

関連する問題