Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show activity indicator while tableView loads?

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 function:

func showActivityIndicator() {     dispatch_async(dispatch_get_main_queue()) {         self.spinner = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge)         self.spinner.frame = CGRect(x: 0.0, y: 0.0, width: 80.0, height: 80.0)         self.spinner.center = CGPoint(x:self.loadingView.bounds.size.width / 2, y:self.loadingView.bounds.size.height / 2)          self.loadingView.addSubview(self.spinner)         self.view.addSubview(self.loadingView)         self.spinner.startAnimating()     } } 

that will show my indicator. And try to use it when I tapped from my infoController to button:

@IBAction func goToMainFromInfo(sender: AnyObject) {         self.showActivityIndicator()         self.performSegueWithIdentifier("fromMainToInfoWActivity", sender: nil)         self.hideActivityIndicator()     } } 

I show it before segue perform and hide after. It doesn't help me. When I did try to use sync:

@IBAction func goToMainFromInfo(sender: AnyObject) {     dispatch_async(dispatch_get_main_queue()) {         self.showActivityIndicator()         self.performSegueWithIdentifier("fromMainToInfoWActivity", sender: nil)         self.hideActivityIndicator()     } } 

But it doesn't help too. When I press to tab it opacity becomes 0.5 and I wait while it loading. But I do not see my activity indicator.

What is the problem?

like image 922
Orkhan Alizade Avatar asked Apr 28 '15 07:04

Orkhan Alizade


People also ask

How will you know the end of loading of UITableView?

willDisplayCell: used here for smoother UI (single cell usually displays fast after willDisplay: call). You could also try it with tableView:didEndDisplayingCell: . Much better for knowing when all the cells load that are visible. However this will be called whenever the user scrolls to view more cells.

What is indexPath in tableView Swift?

Swift version: 5.6. Index paths describe an item's position inside a table view or collection view, storing both its section and its position inside that section.


2 Answers

Just try this:

var indicator = UIActivityIndicatorView()  func activityIndicator() {     indicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 40, 40))     indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray     indicator.center = self.view.center     self.view.addSubview(indicator)     } 

And where you want to start animating

indicator.startAnimating() indicator.backgroundColor = UIColor.whiteColor() 

For stop:

indicator.stopAnimating() indicator.hidesWhenStopped = true 

Note: Avoid the calling of start and stop at the same time. Just give some conditions.

SWIFT : 4.2 Just try this:

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

And where you want to start animating

activityIndicator() indicator.startAnimating() indicator.backgroundColor = .white 

For stop:

indicator.stopAnimating() indicator.hidesWhenStopped = true 
like image 149
Lydia Avatar answered Oct 05 '22 20:10

Lydia


Swift 3.0

// UIView Extension

fileprivate var ActivityIndicatorViewAssociativeKey = "ActivityIndicatorViewAssociativeKey" public extension UIView {    var activityIndicatorView: UIActivityIndicatorView {         get {             if let activityIndicatorView = getAssociatedObject(&ActivityIndicatorViewAssociativeKey) as? UIActivityIndicatorView {                 return activityIndicatorView             } else {                 let activityIndicatorView = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))                 activityIndicatorView.activityIndicatorViewStyle = .gray                 activityIndicatorView.color = .gray                 activityIndicatorView.center = center                 activityIndicatorView.hidesWhenStopped = true                 addSubview(activityIndicatorView)                  setAssociatedObject(activityIndicatorView, associativeKey: &ActivityIndicatorViewAssociativeKey, policy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC)                 return activityIndicatorView             }         }          set {             addSubview(newValue)             setAssociatedObject(newValue, associativeKey:&ActivityIndicatorViewAssociativeKey, policy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC)         }     } } 

// NSObject Extension

public extension NSObject {     func setAssociatedObject(_ value: AnyObject?, associativeKey: UnsafeRawPointer, policy: objc_AssociationPolicy) {         if let valueAsAnyObject = value {             objc_setAssociatedObject(self, associativeKey, valueAsAnyObject, policy)         }     }      func getAssociatedObject(_ associativeKey: UnsafeRawPointer) -> Any? {         guard let valueAsType = objc_getAssociatedObject(self, associativeKey) else {             return nil         }         return valueAsType     } } 

start animation

tableView.activityIndicatorView.startAnimating()

stop animation

tableView.activityIndicatorView.stopAnimating()

You can find more code in Magic

like image 42
broccoli Avatar answered Oct 05 '22 21:10

broccoli