2017-12-19 4 views
0

私はUICollectionViewを持っています。私は行0以外のすべての行にロック画像を追加しました。ViewControllerがロードされるとうまくいきますが、水平にスクロールすると、行0のロック画像が表示されます。ありがとうございました。ここでスクロール時にUICollectionViewの行0からロックイメージを非表示にする方法swift3

は私のコードです: -

var imageView1 = UIImageView() 

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell 
    cell.label.text = tittle[indexPath.row] 

    cell.imageView.image = UIImage(named : image[indexPath.row]) 

    imageView1 = UIImageView(frame:CGRect(x :cell.frame.size.width/2 - 30 ,y : 40, width : 30, height : 30)); 

    imageView1.image = UIImage(named: "lock.png") 
    imageView1.image = imageView1.image!.withRenderingMode(.alwaysTemplate) 
    imageView1.tintColor = UIColor.white 

    if (indexPath.row == 0) { 
     imageView1.isHidden = true 
     imageView1.removeFromSuperview() 
    } else { 
     cell.imageView.addSubview(imageView1) 
     if (RemoteModel.sharedInstanceRemoteModel.purchased){ 
      imageView1.isHidden = true 
     } else { 
      imageView1.isHidden = false 
     } 
    } 
    return cell 
} 
+0

コードを表示してください。 –

+0

コードバディを追加しました。 –

+0

UIImageViewの中にSubview UIImageViewを追加する理由 –

答えて

1

ここでは、カスタム・コレクション・ビューのセルを使用していますCollectionViewCell

たのは、だから、

をコンセントにそれをconnnectその後、カスタムセルXIBやストーリーボードプロトタイプセルにあなたがそれ

を設計している場所にいることを追加してみてくださいと

その後条件

そして、最初にこれは私のために働いている

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

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

    cell.label.text = tittle[indexPath.row] 

    cell.imageView.image = UIImage(named : image[indexPath.row]) 

    if (indexPath.item == 0) 
    { 
     cell.imageView1.isHidden = true 
    } 
    else 
    { 
     if (RemoteModel.sharedInstanceRemoteModel.purchased) 
     { 
      cell.imageView1.isHidden = true 
     } 
     else 
     { 
      cell.imageView1.isHidden = false 
     } 
    } 
    return cell 
} 
+0

しかし、これでも作業する必要があります。 –

+0

はい、それはあなたのケースでは動作していないはずですので別のapporachを試してください –

+0

おかげで、それは今働いています。ストーリーボードからの画像ビューに小さな画像を追加しました。最初に隠すよりも、ロック画像イメージを置くよりも、機能しています。 –

0

あなたが直接セルにこれを追加する場合は、このメソッドのcell.imageView.addSubview(imageView1)

cell.imageViewにUIImageView imageView1を追加しますimageView1は前のセルからの参照を持っていないので、スーパービューから削除する必要があります、私たちは、セルの再利用を使用しています。あなたは上記の溶液に行くことができるか、カスタムセルのためにロック画像を追加する必要があると非表示/を維持するのいずれかについて はでを示しています。

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

+0

私がスクロールしているときに問題が発生しています。私はimageView1も追加しました。ifブロック内のremoveFromSuperview()。それは動作しません。あなたは 'cell.imageView'と '場合(indexPath.row == 0){ cell.lockImage.isHidden =真 }他{ セルのような画像オブジェクトを作成するカスタムセルに画像を追加して –

+0

を試しました.lockImage.isHidden = false } ' –

+0

バディビューが最初にロードされたとき、スクロールしているときに、場所ロックイメージが来ているときよりも、バディです。これは私の問題です。 –

0

XIBやストーリーボードの中に隠され、それを維持してみてくださいに応じてその画像ビューを非表示解除/非表示にしてみてください、私は、小さな画像を追加しましたストーリーボードからの画像ビューでは、最初に隠すよりも、ロック画像イメージを置くよりも、動作しています。

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


     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell 
     cell.label.text = tittle[indexPath.row] 

     cell.imageView.image = UIImage(named : image[indexPath.row]) 

     imageView1 = UIImageView(frame:CGRect(x :cell.frame.size.width/2 - 30 ,y : 40, width : 30, height : 30)); 

     cell.lockiconmindcultivation.isHidden = false 
      cell.lockiconmindcultivation.image = UIImage(named: "lock.png") 
      cell.lockiconmindcultivation.image = cell.lockiconmindcultivation.image!.withRenderingMode(.alwaysTemplate) 
      cell.lockiconmindcultivation.tintColor = UIColor.white 

     if (indexPath.row == 0){ 

      cell.lockiconmindcultivation.isHidden = true 
      } 

      cell.lockiconmindcultivation.isHidden = false 
      cell.lockiconmindcultivation.image = UIImage(named: "lock.png") 
      cell.lockiconmindcultivation.image = cell.lockiconmindcultivation.image!.withRenderingMode(.alwaysTemplate) 
      cell.lockiconmindcultivation.tintColor = UIColor.white 

      if (RemoteModel.sharedInstanceRemoteModel.purchased){ 
       cell.lockiconmindcultivation.isHidden = true 
      }else{ 
       cell.lockiconmindcultivation.isHidden = false 
      } 
     } 

     return cell 
     } 
0

あなたのストーリーボードにロック画像ビューを追加する必要がありますが、あなたがコードから、それを追加したい場合は、あなたのCollectionViewCellにロック画像ビューを追加する必要があり、ちょうどcellForItemAtからそれを再表示/非表示にします。あなたのセルはストーリーボードやペン先でデザインされていると思います。

class CollectionViewCell : UICollectionViewCell { 
    var lockImageView: UIImageView? 
    override func awakeFromNib() { 
     lockImageView = UIImageView(frame:CGRect(x :self.frame.size.width/2 - 30 ,y : 40, width : 30, height : 30)); 
     lockImageView?.image = UIImage(named: "lock.png")!.withRenderingMode(.alwaysTemplate) 
     lockImageView.tintColor = UIColor.white 
     self.contentView.addSubview(lockImageView!) 
    } 
} 

cellForItemAtで非表示にするだけです。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell 
    cell.label.text = tittle[indexPath.row] 
    cell.imageView.image = UIImage(named : image[indexPath.row]) 

    if (indexPath.row == 0) { 
     cell.lockImageView.isHidden = true 
    } else { 
     if (RemoteModel.sharedInstanceRemoteModel.purchased){ 
      cell.lockImageView.isHidden = true 
     } else { 
      cell.lockImageView.isHidden = false 
     } 
    } 
    return cell 
} 

cellForItemAtの方法では、ビューを追加して削除するのは効率的ではありません。また、あなたはUIImageViewにビューを追加するはずがありません。

+0

ありがとうございました。私は同じことをし、それも働いています。 –

関連する問題