2016-05-10 5 views
1

HomeCollectionViewを長押ししたときに、HomeCollectionViewCellコントローラのisEditableプロパティを変更したいとします。ただし、cellForItemAtIndexPathメソッドでのみ設定できます。 handleLongPressメソッドでそれらを設定したいと思います。私のUICollectionViewCellのプロパティを変更するには?

func editVideoFiles(sender: UILongPressGestureRecognizer) { 
    if sender.state == UIGestureRecognizerState.Began{ 
     let p:CGPoint = sender.locationInView(self.collectionView) 
     let index: NSIndexPath = self.collectionView.indexPathForItemAtPoint(p)! 
     if let cell = self.collectionView.cellForItemAtIndexPath(index) { 
      //cell is the UICollectionViewCell, I do not know how to set the isEditable property by this. 
     } 
    } 
} 

答えて

0

SOLUTION:

func editVideoFiles(sender: UILongPressGestureRecognizer) { 
    if sender.state == UIGestureRecognizerState.Began{ 
     let p:CGPoint = sender.locationInView(self.collectionView) 
     let index: NSIndexPath = self.collectionView.indexPathForItemAtPoint(p)! 
     if let cell = self.collectionView.cellForItemAtIndexPath(index) as? HomeCollectionViewCell { 
      cell.isEditable = true. 
     } 
    } 
} 

ITワークスWHY:(あなたがそこにそれを定義しているため)

あなたisEditableプロパティは、あなたのHomeCollectionViewCellクラスに存在します。

他のクラスのインスタンスにそのプロパティを呼び出すことで、別のクラスのプロパティにアクセスできます。あなたはclass.propertyが働くだろうと考えているでしょう(ここには:HomeCollectionViewCell.isEditable)。

しかし、クラスのインスタンスのプロパティを呼び出す必要があります。つまり、HomeCollectionViewCell.isEditableは機能しません。

let cell: HomeCollectionViewCell

だからあなたが行うことができます:あなたは、最初のクラスのインスタンスを作成する必要が

cell.isEditable

をので、キャストすることによってとして(本来はCollectionViewCellとして作成された)あなたのcell HomeCollectionViewCellを呼び出すと、cellisEditableを呼び出して、次のようにできる:

cell.isEditable = true

+0

ありがとうございます!しかし私はすべての私の目に見える細胞の特性を設定したい。 collectionView.visibleCells()でセルを使用すると、CellをHomeCollectionViewCellにキャストできません。どうすればいいか教えてください。 – leuction

+0

私はその問題を解決しました。セル(self.collectionView.visibleCells()?[HomeCollectionViewCell])内のセルのためだけに言ってください! – leuction

-1

あなたはHomeCollectionViewCellにセルをキャストする必要があり、その後、あなたはあなたの財産を使用することができます:ここ

はHomeCollectionViewCell

ここ
var isEditable:Bool = false { 
    didSet { 
     updateUI() 
    } 
} 

func updateUI(){ 
    videoImage.image! = videoInformation.featuredImage 
    videoImage.contentMode = .ScaleAspectFill 
    videoTitle.text! = videoInformation.title 
    if isEditable { 
     deleteButton.hidden = false 
    } else { 
     deleteButton.hidden = true 
    } 
} 

で私の財産は私のhandleLongPress方法です。

if let cell = self.collectionView.cellForItemAtIndexPath(index) as? HomeCollectionViewCell { 
    cell.isEditable = true; 
} 
関連する問題