11

I have a UITableView which from an external RSS feed.

When you select a row it uses navigationController and slides in from the right, the problem is that the RSS feed contains images therefore it can can take a few seconds to load and without any indication of what is going on you can mistake it for an application crash.

I decided to add a spinner so that you know that new page is loading.

Here is my code:

RootViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Loading New Page");

[tableView deselectRowAtIndexPath:indexPath animated:YES];
DetailsViewController *detailViewController = [[DetailsViewController alloc] initWithNibName:@"DetailsViewController" bundle:nil];
detailViewController.item = [rssItems objectAtIndex:floor(indexPath.row/2)];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.center = CGPointMake(160, 240);
[self.view addSubview:spinner];
[spinner startAnimating];
[spinner release];

}

DetailsViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

        NSString *imgURL = [item objectForKey:@"image"];
        NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imgURL]];
        item_photo.image = [[UIImage alloc] initWithData:mydata];

    item_title.text = [item objectForKey:@"title"];
    item_date.text = [NSString stringWithFormat:@"Date: %@",[item objectForKey:@"date"]];
    item_time.text = [NSString stringWithFormat:@"Time: %@",[item objectForKey:@"time"]];
    item_cost.text = [NSString stringWithFormat:@"Cost: £%@",[item objectForKey:@"cost"]];
    item_info.text = [item objectForKey:@"description"];
    self.navigationItem.title = @"Event Type";
}

There are two problems with this code.

  1. The Spinner does not active until after the new page has loaded.
  2. The Spinner does not disable once loaded.

If anyone could help me with this problem i would be truly gratefully.

3
  • I think you're still missing the code where then activity indicator view gets stopped/removed. Also: how are you loading the images? If you are using a blocking method (i.e. one that does not run in a background thread), your whole main loop will be blocked which means: no user interaction, no animation etc (which would explain this behaviour). If this is the case you really should switch to asynchronous loading. Jan 24, 2012 at 16:31
  • @TriPhoenix How do you know if it is a blocking method or not? i have added in the code on DetailsViewController.m
    – Eli Stone
    Jan 24, 2012 at 16:43
  • You can tell be the way the function works: blocking functions will return the value immediately, in this case initWithContentsOfURL will have put the data in your NSDataso the funciton cannot return before having returned this data. Asynchronous requests are more complex in the sense that you start the request and won't have the result returned but rather the request will call a callback function on your code when it is finished. There are some frameworks like ASIHTTPRequest which make this easier or you can use NSURLConnection and do it on your own. Jan 24, 2012 at 16:51

3 Answers 3

26

You are adding the activity indicator view to the view of the controller which is pushing the detail view controller, so you wont see it anyway

try moving the second group of code to the viewDidLoad method of DetailsViewController, you can call stopAnimating on the activity indicator when you are finished loading. To get a reference to the UIActivityIndicator you should add a tag

e.g. in viewDidLoad

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.center = CGPointMake(160, 240);
spinner.tag = 12;
[self.view addSubview:spinner];
[spinner startAnimating];
[spinner release];

in the loadingFinished method (whichever method is called when finished loading)

[[self.view viewWithTag:12] stopAnimating];
4
  • This does not work because DetailsViewController does not 'show' until all the information is done loading.
    – Eli Stone
    Jan 24, 2012 at 16:34
  • the details view does not show because you are loading the data synchronously, which blocks the thread. you need to perform this request in the background if you want to show a animating activity indicator
    – wattson12
    Jan 24, 2012 at 16:46
  • 1
    check out the URL programming guide ... and then once you've read that, use something like RestKit or ASIHTTPRequest. Basically you want to start the request when the view appears, show some indication the data is loading, then when the request is finished, display the downloaded data and remove the loading indication
    – wattson12
    Jan 24, 2012 at 16:51
  • This worked great for me, but I needed to add it to master-detail application. I ended up loading the spinner the same way into the master view (not as a subview of the tableview) Then once my list finished loading I removed the spinner from the master view using [[self.view viewWithTag:12] removeFromSuperview];, I was recieving an error for the stopAnimating call because [self.view viewWithTag:12] was returning a view and not a UIActivityIndicatorView with the method stopAnimating. Mar 19, 2014 at 3:48
3

You need to do some work in a background thread. If the following line is the one that takes the time:

detailViewController.item = [rssItems objectAtIndex:floor(indexPath.row/2)];

Then you could do this in the background with GCD:

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

dispatch_async(dispatch_get_global_queue(0, 0), ^{
    // This is the operation that blocks the main thread, so we execute it in a background thread
    id item = [rssItems objectAtIndex:floor(indexPath.row/2)];

        // UIKit calls need to be made on the main thread, so re-dispatch there
        dispatch_async(dispatch_get_main_queue(), ^{
            detailViewController.item = item;
            [spinner stopAnimating];
        });
});

And +1 to @wattson12 - you need to add the spinner to the new view instead. Alternatively you could add the spinner to the current view, and instead put the pushViewControllercall into your GCD main-queue block.

Final point - you'll want to remove the spinner from its superview once you stop it animating. Alternatively, you can have a single instance of the spinner, and set hidesWhenStopped to YES.

1

This is a spinning wheel over a blurred view in swift:

func blurScence(){
    let blurEffect: UIBlurEffect = UIBlurEffect(style: .Dark)
    let blurView: UIVisualEffectView = UIVisualEffectView(effect: blurEffect)
    blurView.translatesAutoresizingMaskIntoConstraints = false
    blurView.frame = self.view.frame

    let spinner = UIActivityIndicatorView(activityIndicatorStyle:.White)
    spinner.center=blurView.center
    blurView.addSubview(spinner)
    spinner.startAnimating()

    self.view.addSubview(blurView)
}
1
  • I used this method and it is working really well. But when I try to stop the animation by calling blurView.removeFromSuperView() it does not change anything. Any advice?
    – progNewbie
    Oct 15, 2018 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.