How to show activity indicator while tableView loads?

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

    I use two extension methods to add an UIActivityIndicatorView as the backgroundView of the tableview.

    extension UITableView {
        func showActivityIndicator() {
            DispatchQueue.main.async {
                let activityView = UIActivityIndicatorView(style: .medium)
                self.backgroundView = activityView
                activityView.startAnimating()
            }
        }
    
        func hideActivityIndicator() {
            DispatchQueue.main.async {
                self.backgroundView = nil
            }
        }
    }
    

    You can show/hide it like this.

    tableView.showActivityIndicator()
    tableView.hideActivityIndicator()
    

提交回复
热议问题