【问题标题】:How to trigger an function of an UICollectionViewCell inside an UICollectionViewController?如何在 UICollectionViewController 中触发 UICollectionView 的功能?
【发布时间】:2019-11-03 15:33:22
【问题描述】:

我想在作为 UICollectionViewController 的 BreathingSwpipingController 内触发 animation3() 函数(这是 PageCell() 中的一个函数,它是一个 UICollectionViewCell)。

class BreathingSwipingController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

var pageCell: PageCell?

覆盖 func viewDidLoad() {

    super.viewDidLoad()

    collectionView.backgroundColor = .white
    collectionView.register(PageCell.self, forCellWithReuseIdentifier: "cellId")
    collectionView.isPagingEnabled = true

    setupStartStopButton()

}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 3
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    pageCell = (collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as? PageCell)!
    return pageCell!
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

@objc func startStopTapped() {

        pageCell!.animation3()

}

}

【问题讨论】:

    标签: swift xcode uicollectionview uikit uicollectionviewcell


    【解决方案1】:

    通过制定协议使PageCell 成为BreathingSwipingController 的代表

    protocol BreathingSwipingDelegate: class {
     func doSth()
    }
    

    现在在UICollectionViewController 中定义一个weak var delegate: BreathingSwipingDelegate?

    然后前往PageCell 文件并遵守该协议

    class PageCell:  BreathingSwipingDelegate .... {
    
    func doSth() {
    animation3()
    
    }
    var mController = BreathingSwipingController()
    override init(style: .....) { 
    mController.delegate = self
    .
    .
    .
    .
    }
    }
    

    现在在BreathingSwipingController 进行以下操作

    @objc func startStopTapped() {
    
            delegate?.doSth()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-06
      • 2013-04-11
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2020-06-28
      相关资源
      最近更新 更多