2016-08-11 14 views
0

cellForItemAtIndexPathを使用してUICollectionViewにセルを追加すると、最初のセルをターゲットにして、セル内の画像に色付きのオーバーレイを追加しようとしています。しかし、対応する回数呼び出されていないように見えても、コードは複数のセルをターゲットにしているように見えます(3つまたは4つのセルにオーバーレイを適用することがありますが、 2回目はreloadData()が呼び出されたときです)。最初のセルにカラーオーバーレイを適用するにはどうすればよいですか?uicollectionview内の特定のセルを変更する

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ProfileCollectionViewCell 

    if(self.userList.count > 0) { 
     cell.frame.size = CGSize(width: 202, height: 207) 
     if(indexPath.item < self.userList.count) { 
      cell.myImage.image = self.userList[indexPath.item].image 
      cell.myImage.layer.cornerRadius = cell.myImage.frame.size.width/2 
      cell.myImage.layer.masksToBounds = true 
      if(indexPath.item == 0) { 
       print("Overlay") 
       let circlePath = UIBezierPath(arcCenter: CGPoint(x: cell.frame.width/2, y: cell.frame.height/2), radius: CGFloat((cell.myImage?.frame.size.width)!/2), startAngle: CGFloat(0), endAngle: CGFloat(M_PI * 2), clockwise: true) 
       let shapeLayer = CAShapeLayer() 
       shapeLayer.path = circlePath.CGPath 
       shapeLayer.fillColor = self.overlayColor.CGColor 

       cell.layer.addSublayer(shapeLayer) 
      } 
     } 
    } 
    return cell 
} 

答えて

1

あなたがitemを使用するかrowあなたは他に置くとCAShapeLayerを削除する必要がある場合、セルが再利用されますので、それ以外の場合は、他のセル項目に表示される問題はありません。コードを以下のものに置き換えてください。

if(indexPath.item == 0) { // if(indexPath.row == 0) { 
    let circlePath = UIBezierPath(arcCenter: CGPoint(x: cell.frame.width/2, y: cell.frame.height/2), radius: CGFloat((cell.myImage?.frame.size.width)!/2), startAngle: CGFloat(0), endAngle: CGFloat(M_PI * 2), clockwise: true) 
    let shapeLayer = CAShapeLayer() 
    shapeLayer.path = circlePath.CGPath 
    shapeLayer.fillColor = self.overlayColor.CGColor 

    cell.layer.addSublayer(shapeLayer) 
} else { 
    let sublayers = cell.layer.sublayers 
    for sublayer in sublayers! { 
     if sublayer.isKindOfClass(CAShapeLayer) { 
      sublayer.removeFromSuperlayer() 
     } 
    } 
} 
+0

ありがとうございました。それはそれを行い、私がリンクされていると思ったいくつかの他の問題を整理するのを助けました。 – Maniacbob

関連する問題