【问题标题】:Swift: collectionviewHeader delegate not working?Swift:collectionviewHeader 委托不起作用?
【发布时间】:2017-01-21 22:57:38
【问题描述】:

好的,我仍然是协议的新手,但我需要从另一个类调用一个函数(我知道在头类中调用该函数时有效)。

为此,在我调用该函数的类(它是一个 MSMessagesAppViewController)中,我有:

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)
    {

它可以编译,但我知道上面的函数没有被调用,因为没有打印语句。我在这里做错了什么?

【问题讨论】:

    标签: swift delegates swift3 protocols


    【解决方案1】:

    几秒钟前我遇到了同样的问题。我想你和我有同样的问题。

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

    类似的东西:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-18
      • 2012-01-07
      • 1970-01-01
      相关资源
      最近更新 更多