简体   繁体   中英

CollectionView cell delegate swift

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??

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM