Search code examples
iosswiftuicollectionviewswift2activity-indicator

Adding Activity/Loading Indicator at the Bottom of UCollectionView


I am wondering what is the best approach to add an Activity Indicator to the bottom of the UICollectionView.

I would like to able to show and display this cell, and I am wondering how should I do this.

I have searched SO and I have found people suggesting adding a new cell to the last row. But I would like to have the instance of the activity indicator so that I can turn it on or off.

Any suggestions?


Solution

  • You can add a new cell to the last row of your collection view and create a custom UICollectionViewCell class for that cell where you can enter the logic of your cell e.g.:

    import UIKit
    
    class LoadingCollectionViewCell: UICollectionViewCell {
    
        @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
    
        override func awakeFromNib() {
            self.activityIndicator.startAnimating()
        }
    
        func stopLoading() {
            self.activityIndicator.stopAnimating()
        }
    }
    

    You can then call the function stopLoading() in your ViewController when you want the activity indicator to stop.