CollectionView小区代表Swift

发布于 2025-02-13 02:46:19 字数 1429 浏览 0 评论 0原文

我有带有CollectionView和uicollectionViewDataSource扩展的ViewController:

 class CheckInViewController: UIViewController {
    let checkInSystem = CheckInSystem()
    let collectionView = UICollectionView()
    
viewDidAppear(){
 super.viewDidAppear(animated)
 checkInSystem.lala() // its function which should call delegate
}


    //...

}

extension CheckInViewController: UICollectionViewDataSource {

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CheckInCell.identifier, for: indexPath) as! CheckInCell

    }

还有协议:

protocol AnimateTheLastCheckInDayDelegate: AnyObject{
    func animateLastDay(_ cell: CheckInCell)
}

并且已经实现了

extension CheckInViewController: AnimateTheLastCheckInDayDelegate{
    func animateLastDay(_ cell: CheckInCell) {
        cell.backgroundColor = .red
        print(1233254234) //works
    }
class CheckInSystem{
    weak var animateTheLastCheckInDayDelegate: AnimateTheLastCheckInDayDelegate?
    func checkInScreenLaunched(){
//...
 func lala(){
    animateTheLastCheckInDay?.animateLastDay(CheckInCell()) 

 }
}

我需要从 AnimAnimateThelastCheckInday?Animimatelastday(checkincell(checkincell())()。但它创建了新的单元格。如何修复?

I have ViewController with collectionView and UICollectionViewDataSource extension:

 class CheckInViewController: UIViewController {
    let checkInSystem = CheckInSystem()
    let collectionView = UICollectionView()
    
viewDidAppear(){
 super.viewDidAppear(animated)
 checkInSystem.lala() // its function which should call delegate
}


    //...

}

extension CheckInViewController: UICollectionViewDataSource {

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CheckInCell.identifier, for: indexPath) as! CheckInCell

    }

Also there are protocol:

protocol AnimateTheLastCheckInDayDelegate: AnyObject{
    func animateLastDay(_ cell: CheckInCell)
}

And it's realisation

extension CheckInViewController: AnimateTheLastCheckInDayDelegate{
    func animateLastDay(_ cell: CheckInCell) {
        cell.backgroundColor = .red
        print(1233254234) //works
    }
class CheckInSystem{
    weak var animateTheLastCheckInDayDelegate: AnimateTheLastCheckInDayDelegate?
    func checkInScreenLaunched(){
//...
 func lala(){
    animateTheLastCheckInDay?.animateLastDay(CheckInCell()) 

 }
}

I need to change cell background from animateTheLastCheckInDay?.animateLastDay(CheckInCell()) . But It creates new cell. How to fix??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚风撩人 2025-02-20 02:46:20

这将创建一个新的单元格checkincell()

animateTheLastCheckInDay?.animateLastDay(CheckInCell()) 

您需要通过单元格,因此请添加委托方法的实现willdisplay with with

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell,  forItemAt indexPath: IndexPath)  {
    checkInSystem.lala(cell)
} 

with code

func lala(_ cell: CheckInCell)
  animateTheLastCheckInDay?.animateLastDay(cell)  
}

This creates a new cell CheckInCell()

animateTheLastCheckInDay?.animateLastDay(CheckInCell()) 

You need to pass the cell so add implementation for delegate method willDisplay

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell,  forItemAt indexPath: IndexPath)  {
    checkInSystem.lala(cell)
} 

With

func lala(_ cell: CheckInCell)
  animateTheLastCheckInDay?.animateLastDay(cell)  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文