2017-12-19 4 views
0

UICollectionView가 있는데 완벽하게 작동합니다. 나는 행 0 제외한 모든 행에 잠금 이미지를 추가했습니다. ViewController로드 될 때 잘 작동하지만 가로로 스크롤 할 때 행 0 잠금 이미지를 표시합니다. 내가 뭘 잘못하고있다? advace에 감사드립니다. 여기 스크롤 할 때 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 또는 스토리 보드 프로토 타입 셀

그 후

그리고 처음에 그것이 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에 이전 셀의 참조가 없으므로 superview에서 제거해야하므로 셀 재사용을 사용합니다. 은 어느 위해 당신은 위의 솔루션으로 갈 수 또는 사용자 정의 셀에있는 잠금 이미지를 추가 할 필요 숨기기/가에 표시 유지 보수 : 여기

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

+0

스크롤 할 때 문제가 발생했습니다. 나는 imageView1도 추가했다.if 블록에서 removeFromSuperview()를 호출합니다. 작동하지 않습니다. –

+0

당신이 이미지 cell.imageView''와 같은 객체와 을 만들 사용자 정의 셀에 이미지를 추가로 시도 했'경우 (indexPath.row == 0) { cell.lockImage.isHidden = 사실 } 다른 { 세포 .lockImage.isHidden = false } ' –

+0

버디 뷰가 처음로드 될 때, 내가 스크롤 할 때 위치 잠금 이미지보다 오히려 숨어 있습니다. 이게 내 문제 야. –

0

, 스토리 보드의 이미지보기에서 잠금 이미지 이미지를 넣는 것보다 처음에 숨기도록하는 것보다 효과적입니다.

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

고마워 친구. 나는 똑같이했고 또한 일했다. –

관련 문제