2016-05-13 44 views
0

我使用主類調用newsFeedCointroller作爲UICollectionViewController。 1.在單元格內,我有一個類似按鈕的新聞源(填充單元格我使用一個名爲「FeedCell」的類) 2.從單元格中(在mainview中)我有一個標籤(labelX)用於「 「與一個函數稱爲」messageAnimated「如何從單元格中的按鈕調用外部函數

如何從單元格內的按鈕調用」messageAnimated「功能。

我想對標籤文字更改爲舉例說:「你只是喜歡它」 ......

謝謝你幫我

+0

你可以把對哪裏是你的「按鈕」一些代碼,這裏是你的函數「messageAnimated」 –

回答

2

在你FeedCell你應該聲明一個代理(閱讀關於委託模式here

protocol FeedCellDelegate { 
    func didClickButtonLikeInFeedCell(cell: FeedCell) 
} 

在你的實現(假設你手動添加目標)

var delegate: FeedCellDelegate? 

override func awakeFromNib() { 
    self.likeButton.addTarget(self, action: #selector(FeedCell.onClickButtonLike(_:)), forControlEvents: .TouchUpInside) 
} 

func onClickButtonLike(sender: UIButton) { 
     self.delegate?.didClickButtonLikeInFeedCell(self) 
} 

在您的視圖控制器

extension FeedViewController: UICollectionViewDataSource, UICollectionViewDelegate { 
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("feedCell", forIndexPath: indexPath) as! FeedCell 
     // Do your setup. 
     // ... 
     // Then here, set the delegate 
     cell.delegate = self 
     return cell 
    } 

    // I don't care about other delegate functions, it's up to you. 
} 

extension FeedViewController: FeedCellDelegate { 
    func didClickButtonLikeInFeedCell(cell: FeedCell) { 
     // Do whatever you want to do when click the like button. 
     let indexPath = collectionView.indexPathForCell(cell) 
     print("Button like clicked from cell with indexPath \(indexPath)") 
     messageAnimated() 
    } 
} 
+0

我索裏我是新來的,你可以幫我一個小(再一次,索里爾,非常感謝你幫助我) https://docs.google.com/document/d/1yGrYd6JFQU_xsqWEK_22H14nSTtc7TSUQxOFYtG-hp8/pub –

+0

不客氣。但是,我怎麼幫你? – Tien

相關問題