Place activity indicator over uitable view
Here's the steps: Followed Chrissukhram instructions.
- drag a view on top of table view Step
- drag activity
indicator on to the view Align the activity indicator to
horizontal and vertical centre in containermake IBOutlet
connection for both view and activity indicator- start the
activity indicator - stop and hide :
activityIndicatorLarge.hidesWhenStopped = true
activityView.hidden = true
Activity Indicator While Tableview Loads
It seems you have an asynchronous function for loading the table view data. You just need to put activityIndicator.stopAnimating()
inside there.
Add a UIActivityIndicatorView
either programatically or through Storyboard
to your table view controller.
If you choose to do it in code, this is how you do it, including adding constraints to keep it in the middle of your view controller:
view.addSubview(activityIndicator)
activityIndicator.translatesAutoresizingMaskIntoConstraints = false
activityIndicator.hidesWhenStopped = true
activityIndicator.color = UIColor.black
let horizontalConstraint = NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0)
view.addConstraint(horizontalConstraint)
let verticalConstraint = NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0)
view.addConstraint(verticalConstraint)
Before calling the above code inside viewDidLoad()
, declare let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
somewhere in your class.
You just have to start animating it as soon as loadAnimals
starts execution and stop it before calling tableView.reloadData()
. Name it `activityIndicator, then do the following:
func loadAnimals() {
activityIndicator.startAnimating()
let animalQuery = PFQuery(className: "Animals")
animalQuery.whereKey("userID", equalTo: PFUser.current()?.objectId! ?? String()) //getting which user
animalQuery.limit = 10
animalQuery.findObjectsInBackground { (objects, error) in
if error == nil {
self.colorArray.removeAll(keepingCapacity: false)
self.animalNameArray.removeAll(keepingCapacity: false)
self.animalTypeArray.removeAll(keepingCapacity: false)
for object in objects! {
self.colorArray.append(object.value(forKey: "colorType") as! String) // add data to arrays
self.animalNameArray.append(object.value(forKey: "animalName") as! String) // add data to arrays
self.animalTypeArray.append(object.value(forKey: "animalType") as! String) // add data to arrays
}
activityIndicator.stopAnimating()
self.tableView.reloadData()
} else {
print(error?.localizedDescription ?? String())
}
}
}
Centering Activity Indicator View to UITableView
The problem you have is that a UITableViewController handles a single tableView, tableViews are special kind of scrollviews that change depending on the contents. Adding stuff to tableviews is very uncommon, which is why you use prototype cells and you change these cells. There is no "unhacky" way to do it.
The proper way to do this is, have a normal ViewController with a tableview, and a view with the indicator. THEN make the viewcontroller conform to the datasource and delegate protocols for handling a tableview.
If you still want to use a tableviewcontroller, this is the so so less hacky method:
UITableView Activity Indicator the Apple way
Add a ActivityIndicator to the bottom of UITableView while loading
Add This Function
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let lastSectionIndex = tableView.numberOfSections - 1
let lastRowIndex = tableView.numberOfRows(inSection: lastSectionIndex) - 1
if indexPath.section == lastSectionIndex && indexPath.row == lastRowIndex {
// print("this is the last cell")
let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)
spinner.startAnimating()
spinner.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: tableView.bounds.width, height: CGFloat(44))
self.tableview.tableFooterView = spinner
self.tableview.tableFooterView?.isHidden = false
}
}
and tableFooterView should hide when data load.
How can i add a activity indicator below the UITableView?
The best way to add activity indicator is at footer of tableview.
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *headerView = [[[UIView alloc] init]autorelease];
[headerView setBackgroundColor:[UIColor clearColor]];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Load More" forState:UIControlStateNormal];
button.frame = CGRectMake(10.0, 210.0, 160.0, 40.0);
[headerView addSubview:button];
[headerLabel release];
return headerView;
}
Add a button to the footerView and set action to button (as you needed). This how app store tableview works. On clicking button fetch some more data add to table array scroll the table without animation.
How to put activityindicator middle of a tableview
try with bellow code:-
spinner.center = CGPointMake( [UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2);
yourAppdelegateClass *appDelegate = (yourAppdelegateClass*)[[UIApplication sharedApplication] delegate];
[appDelegate.window addSubview:spinner];
Code Output is:-
Display Activity Indicator in tableview cell
You could do like below, let me know if that works for you, you can modify the frame
as per your requirement.
func downloadImage(url: NSURL, type: String) {
let activityIndicator = UIActivityIndicatorView()
activityIndicator.frame = self.mainImage.bounds
getDataFromUrl(url) { (data, response, error) in
//show activity indicator
self.mainImage.addSubview(activityIndicator)
activityIndicator.startAnimating()
dispatch_async(dispatch_get_main_queue()) { () -> Void in
guard let data = data where error == nil else { return }
activityIndicator.stopAnimating()
activityIndicator.removeFromSuperview()
self.mainImage.image = UIImage(data: data)
}
}
}
Related Topics
Find Multiple Quoted Words in a String with Regex
How to Check If a Property Has Been Set Using Swift Reflection
How to Generate a Binding for Each Array Element
Nsjsonserialization Not Working as Expected in a Playground
Swift - Get File Path of Currently Opened Document in Another Application
How Do Generators Whose Element Is Optional Know When They'Ve Reached the End
How to Say "If X == a or B or C" as Succinctly in Swift as Possible
Compare Three Values for Equality
How to Cast an Any Value with Nil in It to a Any
Many Ways of Defining a Swift Dictionary
How to Animate Bordercolor Change in Swift
Nsurlerrordomain with Code=-1100
Get Lat and Long from Tapped Overlay in Google Maps
Nothing Prints Out in the Console in Command Line Tool Xcode
Session.Datataskwithurl Completionhandler Never Called
Pattern Match and Conditionally Bind in a Single Switch Statement