Ios 如何从单元格中的按钮调用外部函数

Ios 如何从单元格中的按钮调用外部函数,ios,xcode,swift2,uicollectionview,uicollectionviewcell,Ios,Xcode,Swift2,Uicollectionview,Uicollectionviewcell,我使用一个主类调用newsfeedconcontroller作为UICollectionViewController。 1.在cell inside中,我有一个带有like按钮的新闻提要(为了填充单元格,我使用了一个名为“FeedCell”的类) 2.从单元格中(在mainview中)我有一个标签(labelX),用于“splash message”,带有一个名为“messageAnimated”的函数 如何从单元格内的按钮调用“messageAnimated”函数 我想将标签文本更改为例如:“

我使用一个主类调用
newsfeedconcontroller
作为
UICollectionViewController
。 1.在cell inside中,我有一个带有like按钮的新闻提要(为了填充单元格,我使用了一个名为“FeedCell”的类) 2.从单元格中(在mainview中)我有一个标签(labelX),用于“splash message”,带有一个名为“messageAnimated”的函数

如何从单元格内的按钮调用“messageAnimated”函数

我想将标签文本更改为例如:“你只是喜欢它”


感谢您在FeedCell中帮助我

,您应该声明一个委托(阅读委托模式)

在单元实现中(假设手动添加目标)

在视图控制器中

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()
    }
}

在FeedCell中,您应该声明一个委托(阅读委托模式)

在单元实现中(假设手动添加目标)

在视图控制器中

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()
    }
}

你能为你的“按钮”和功能“messageAnimated”在哪里写一些代码吗?你能为你的“按钮”和功能“messageAnimated”在哪里写一些代码吗?对不起,我是新来的,你能再帮我一点吗(再次,对不起,非常感谢你的帮助)对不起,我是新来的,你能再帮我一点吗(再一次,对不起,非常感谢你帮我)
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()
    }
}