2016-06-28 4 views
1

UICollectionViewのカスタムセルにチェックマークを表示する際に問題があります。最初のいくつかのタップでは、すべてが期待通りに機能しますが、スクロールやタップを繰り返したり、選択したセルをクリックしたりすると、gifのように動作が奇妙になります。おそらく私はこれについて間違った方法で行くつもりですか? .addCheck()と.removeCheck()は、私が作成したカスタムUICollectionViewCellクラス内のメソッドであり、チェックマークイメージを追加するか、セルビューから削除するだけです。以下はThe odd behavior shown hereUICollectionViewでスクロールして選択するときの奇妙な動作

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

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

    // Configure the cell 
    let color = colorList[(indexPath as NSIndexPath).row] 
    cell.delegate = self 
    cell.textLabel.text = color.name 
    cell.backgroundColor = color.color 

    if color.selected { 
     cell.addCheck() 
    } 
    else { 
     cell.removeCheck() 
    } 

    return cell 
} 

// user selects item 
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

    // set colors to false for selection 
    for color in colorList { 
     color.selected = false 
    } 

    // set selected color to true for selection 
    let color = colorList[indexPath.row] 
    color.selected = true 
    settings.backgroundColor = color.color 
    //userDefaults.set(selectedIndex, forKey: "selectedIndex") 
    collectionView.reloadData() 
} 

は私のカスタムセル内addCheck()とremoveCheck()関数がどのように見えるかです。最初

func addCheck() { 
    // create check image 
    let checkImage = UIImage(named: "checkmark") 
    checkImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: bounds.size.height/4, height: bounds.size.height/4)) 
    checkImageView.image = checkImage!.withRenderingMode(UIImageRenderingMode.alwaysTemplate) 
    checkImageView.tintColor = UIColor.white() 

    // add the views 
    addSubview(checkImageView) 
} 

func removeCheck() { 
    if checkImageView != nil { 
     checkImageView.removeFromSuperview() 
    } 
} 
+0

チェックマークを使用すると、私は 'UITableView'と同様の問題を抱えています。行とセクションの値を格納するタプルの配列を作成してみてください。 http://stackoverflow.com/questions/27918584/uitableview-checkmarks-disappear-when-scrolling – Sami

答えて

1

オフ、あなたは少しあなたのdidSelectを簡素化することができます:あなたのcellForItemAt方法で言語に基づいて

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

    // set colors to false for selection 
    for (index, color) in colorList.enumerate() { 
     if index == indexPath.row { 
      color.selected = false 
      settings.backgroundColor = color.color 
     } 
     else { 
      color.selected = false 
     } 
    } 

    collectionView.reloadData() 
} 

、私はあなたがタップしたときに第2のチェックマーク画像を追加している推測しています同じセルに2回、正しくトラッキングされていないので、セルがちょうどコレクションビューがリロードされたあと、周りを回転し続けるようになる

あなたのセルクラス、または少なくともaddCheckとremoveCheckのロジックが投稿されています。

私が推奨することは、選択に基づいて単純に表示/非表示にするときに、セル上にチェックマークが付いた画像を永久に保持することです。これにより、collectionViewも高速化するはずです。

+0

ありがとうございます。 addCheck()とremoveCheck()関数をquesitonに追加しました。 – Lee

+0

私は各セルのチェックマークをオーバーレイする方法を試してみましたが、イメージを初期化してから、イメージを削除するのではなく隠してしまい、正常に動作しているようです。どうもありがとうございました! – Lee

+1

素晴らしい!周囲に浮かぶ複数の画像で終わったように見えます – PeejWeej

関連する問題