Search code examples
iosswiftuicollectionviewuiactivityindicatorviewuicollectionreusableview

UIActivityIndicatorView disappears in CollectionView Footer


I have a collectionView with only 1 section in my app which download data from API. I have a pagination and I am trying to add a loading footer in my collectionView. The header appears normally. I have in this footer cell an UIActivityIndicatorView and a UILabel. When the first at a limit is triggered, the 2 elements are present in the cell but when the second limit is triggered, the UIActivityIndicatorView is absent.

Have you an idea for that.

The code of the cell (BaseCell is just a class to avoid tapping the init and required init each time):

class loadingCell: BaseCell {

    override func setupViews() {
        super.setupViews()

        backgroundColor = .yellow

        let activity = UIActivityIndicatorView()
        activity.backgroundColor = .red
        activity.startAnimating()
        activity.frame = CGRect(x: 10, y: 20, width: 20, height: 20)
        let label = UILabel()
        label.text = "hello"
        label.frame = CGRect(x: 100, y: 20, width: 40, height: 20)
        label.backgroundColor = .green


        addSubview(activity)
        addSubview(label)

    }

}

The collection delegate methods :

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
     return CGSize(width: SCREENW, height: 50)
}

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

    if kind == UICollectionElementKindSectionFooter {
        let loadingFooterView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionFooter, withReuseIdentifier: loadingCell, for: indexPath)
        return loadingFooterView
    }

    return UICollectionReusableView()
}

The cell is well registered.

What happens when first limit is triggered:

enter image description here

And with the second:

enter image description here


Solution

  • Based on @MaksymMusiienko comment, I have tried this which do the job to reset the animation of the spinner.

    class loadingCell: BaseCell {
    
        let activitySpinner: UIActivityIndicatorView = {
           let spinner = UIActivityIndicatorView()
            spinner.backgroundColor = .red
            spinner.startAnimating()
            spinner.frame = CGRect(x: 10, y: 20, width: 20, height: 20)
            return spinner
    
        }()
    
        override func setupViews() {
            super.setupViews()
    
            backgroundColor = .yellow
    
            let label = UILabel()
            label.text = "hello"
            label.frame = CGRect(x: 100, y: 20, width: 40, height: 20)
            label.backgroundColor = .green
    
    
            addSubview(activitySpinner)
            addSubview(label)
    
        }
    
        override func prepareForReuse() {
            super.prepareForReuse()
            activitySpinner.startAnimating()
        }
    }