Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an UIActivityIndicatorView when loading a UITableViewCell

I have two UITableViewControllers, A and B. When I tap one cell in table A, I will use UINavigationController to push table view controller B. But the data of B is downloaded from Internet, which takes several seconds. So I want to add a UIActivityIndicatorView when loading B. How can I achieve this?

like image 635
Wu Linsen Avatar asked Aug 27 '11 06:08

Wu Linsen


2 Answers

You can add UIActivityIndicatorView as cell's accessoryView.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {     UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];     spinner.frame = CGRectMake(0, 0, 24, 24);     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];     cell.accessoryView = spinner;     [spinner startAnimating];     [spinner release]; } 
like image 95
EmptyStack Avatar answered Sep 20 '22 09:09

EmptyStack


In viewDidLoad of tableview B class, add an activity indicator.

// Create the Activity Indicator.     let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)     activityIndicator.hidesWhenStopped = true     view.addSubview(activityIndicator)      // Position it at the center of the ViewController.     activityIndicator.translatesAutoresizingMaskIntoConstraints = false     NSLayoutConstraint.activate([         activityIndicator.centerXAnchor.constraint(equalTo: view.centerXAnchor),         activityIndicator.centerYAnchor.constraint(equalTo: view.centerYAnchor)])     activityIndicator.startAnimating() 

Now call your method that downloads data from the network.

myDownloadMethod() 

Do it in a different thread if you don't want the UI to be non responsive during the process.

read this thread for that. Can I use a background thread to parse data?

When you are notified that the contents are downloaded, stop the indicator.

activityIndicator.stopAnimating() 

Now you can call tableview.reloadData() for reloading the table to display the new contents.

like image 31
ArunGJ Avatar answered Sep 20 '22 09:09

ArunGJ