2017-03-25 10 views
0

各セルにイメージとボタンが含まれており、それぞれ選択または選択できないコレクションビューを作成しようとしています。最初はすべてがうまくいくように見えますが、いくつかのボタンを押してビューを移動すると、「選択された」ボタンが「選択されていません」に変わり、その逆もあります。スクロール3選択ボタンスクロール時のコレクションビューでのポステイションの変更

人が画像を切り替える際に問題があることがわかりましたので、画像をキャッシュしてみました。私も完全に画像を削除しようとしましたが、問題は残ります。ここで

は私のコレクションビューの設定である:ここで

override func collectionView(_: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

    return imageArray.count 

} 


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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell 

    //cell.photo.image = imageArray[indexPath.row] 

    cell.selectButton.tag = indexPath.row 
    cell.selectButton.addTarget(self, action: #selector(buttonPressed(sender:)), for: .touchUpInside) 

    return cell 

} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

    let length = collectionView.frame.width/3 - 1 


    return CGSize(width: length, height: length) 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 
    //sets spacing between images 
    return 1.0 
} 
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 
    //sets spacing between images 
    return 1.0 
} 

は私のbuttonPressed機能です:

func buttonPressed(sender: UIButton) { 

    if sender.isSelected == true{ 
     sender.setTitleColor(UIColor.blue, for:UIControlState.normal) 
     sender.isSelected = false 
    }else{ 
     sender.setTitleColor(UIColor.red, for:UIControlState.normal) 
     sender.isSelected = true 
    } 
} 

は、これはどのように解決することができますか?

答えて

1

必要なのは、配列を取り、その中のすべての選択したインデックスを保つoverride func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell

でセルを作成中に、ボタン状態を毎回チェックすることです。

var mArray = [Int]() 


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

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell 

     //cell.photo.image = imageArray[indexPath.row] 

     cell.selectButton.tag = indexPath.row 
     cell.selectButton.addTarget(self, action: #selector(buttonPressed(sender:)), for: .touchUpInside) 

//check if indexpath.row is selected 
    if mArray.contains(indexPath.row){ 
       cell.selectButton.setTitleColor(UIColor.red, for:UIControlState.normal) 
       cell.selectButton.isSelected = true 
    }else{ 
       cell.selectButton.setTitleColor(UIColor.blue, for:UIControlState.normal) 
       cell.selectButton.isSelected = false 
    } 


     return cell 

    } 


func buttonPressed(sender: UIButton) { 

    if sender.isSelected == true{ 
     mArray.removeAtIndex(sender.tag) 
     sender.setTitleColor(UIColor.blue, for:UIControlState.normal) 
     sender.isSelected = false 
    }else{ 
     mArray.append = sender.tag 
     sender.setTitleColor(UIColor.red, for:UIControlState.normal) 
     sender.isSelected = true 
    } 
} 
+0

ご協力いただきありがとうございます。私はちょうどこれを再訪し、より徹底的なテストの後では動作しません。間違った項目が配列から削除されたため、 'myArray = myArray.filter {$ 0!= sender.tag}'に 'mArray.removeAtIndex(sender.tag)'をスワップして修正しました。アイテムを選択解除し、選択したボタンの位置をすばやくスクロールすると、何が原因である可能性がありますか? – Tom

関連する問題