2016-05-13 39 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

相关问题