How to show activity indicator while tableView loads?

前端 未结 12 1543
说谎
说谎 2021-01-30 21:44

When I switch between my tabs it loads some seconds and I want to know that my data is loading. For that I decided to add an activity indicator.

I wrote a little functio

12条回答
  •  北恋
    北恋 (楼主)
    2021-01-30 22:26

    Update Swift 4.2:

    1.call the activityIndicator function on viewDidLoad
    eg:

    var indicator = UIActivityIndicatorView()
    override func viewDidLoad() {
        //Initializing the Activity Indicator
        activityIndicator()
        //Starting the Activity Indicator
        indicator.startAnimating()
        //Call Your WebService or API
        callAPI()
    }
    

    Here is the Code For Adding ActivityIndicator as Subview

    func activityIndicator() {
                indicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
                indicator.style = UIActivityIndicatorView.Style.whiteLarge
                indicator.color = .red
                indicator.center = self.view.center
                self.view.addSubview(indicator)
        }
    

    2. Do UI related Operations or API Call and stop activity indicator

        func callAPI() {     
            YourModelClass.fetchResult(someParams, 
            completionHandler: { (response) in 
            //Do necessary UIUpdates
            //And stop Activity Indicator
            self.indicator.stopAnimating()
            })
        }
    

提交回复
热议问题