![](/img/trans.png)
[英]How to call UIView transition on a UIButton image from outside the button?
[英]How to call an outside function from button in cell
我使用主类调用newsFeedCointroller
作为UICollectionViewController
。 1.在内部的单元格中,我有一个带有“喜欢”按钮的新闻源(要填充该单元格,我使用了一个名为“ FeedCell”的类)。2.从单元格中(在主视图中),我有一个标签(labelX)用于“启动消息”,一个名为“ messageAnimated”的函数
如何从单元格内的按钮调用“ messageAnimated”函数。
我想将标签文本更改为例如:“您刚刚喜欢它” ...
谢谢你帮我
在FeedCell中,您应该声明一个委托( 在此处阅读有关委托模式的信息 )
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)
}
在您的View控制器中
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()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.