How to show activity indicator while tableView loads?

前端 未结 12 1546
说谎
说谎 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:10

    Another approach, In my code I added an extension for UITableView (Swift 2.3) :

    extension UITableView {
        func activityIndicator(center: CGPoint = CGPointZero, loadingInProgress: Bool) {
            let tag = 12093
            if loadingInProgress {
                var indicator = UIActivityIndicatorView()
                indicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 40, 40))
                indicator.tag = tag
                indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
                indicator.color = //Your color here
                indicator.center = center
                indicator.startAnimating()
                indicator.hidesWhenStopped = true
                self.superview?.addSubview(indicator)
            }else {
                 if let indicator = self.superview?.viewWithTag(tag) as? UIActivityIndicatorView { {
                    indicator.stopAnimating()
                    indicator.removeFromSuperview()
                }
            }
        }
    }
    

    Note : My tableview is embedded in a UIView (superview)

提交回复
热议问题