Swift:collectionviewHeader委托不起作用? [英] Swift: collectionviewHeader delegate not working?

查看:496
本文介绍了Swift:collectionviewHeader委托不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我对协议仍然很陌生,但是我需要从另一个类调用一个函数(在标头类中被调用时才知道有效)。

Ok, Im still new with protocols but I need to call a function (that I know works when called in the header class) from another class.

要执行此操作,在我从中调用函数的类(其为MSMessagesAppViewController)中,我拥有:

To do this, In the class from which Im calling the function (its a MSMessagesAppViewController) I have:

public protocol MSMessagesAppViewControllerDelegate: class {
    func shrinkHeight(height:CGFloat)
    func growHeight(height: CGFloat)
}

weak var delegate: MSMessagesAppViewControllerDelegate?
delegate?.shrinkHeight(height: 90)

然后我在 class HeaderCollectionReusableView:UICollectionReusableView,MSMessagesAppViewControllerDelegate 实际函数:

 func shrinkHeight(height:CGFloat)
    {
        print("WORKING!!");  //NOT printed

        UIView.animate(withDuration: 0.7, delay: 0.0, usingSpringWithDamping:
            0.6, initialSpringVelocity: 1.1, options: [], animations: {
                //thing being animated

                self.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: self.screenSize.height * (height/self.screenSize.height))

                //change out
                    self.squareLogo.isHidden = true
                    self.logoLabel.isHidden = false
        }, completion: { finished in
            //code that runs after the transition is complete here


        })
    }

    func growHeight(height:CGFloat)
    {

它可以编译,但是我知道上面的函数没有被调用,因为没有打印语句。

It compiles but I know the functions above aren't called because no print statement. What am I doing wrong here?

推荐答案

几秒钟前我遇到了同样的问题。我认为您和我有同样的问题。

I had the same issue few seconds ago. I think you have the same problem as I had.

在实现MSMessagesAppViewControllerDelegate的自定义UICollectionView类中,必须为可重用单元格添加委托。

In you custom UICollectionView class where you implemented MSMessagesAppViewControllerDelegate you have to add the delegate for the reusable cell.

类似的东西:

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    if kind == UICollectionElementKindSectionHeader {
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerCell", for: indexPath) as! YourCustomClassForHeader
        headerView.delegate = self // here was my problem, I just forgot to set the delegate
        return headerView
    } else {
        return UICollectionReusableView()
    }
}

这篇关于Swift:collectionviewHeader委托不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆