ophychius
ophychius

Reputation: 2653

Cannot get UIActivityIndicatorView to show while loading data

I have an UIActivityIndicatorView on a TableViewController. When I start animating in the init I will see the activity indicator, but when I move the start command anywhere else it will not work/show. Does anyone have an idea what I am doing wrong, I cannot seem to find it on here. I want it to show while I am loading data.

TableViewController.h

#import <UIKit/UIKit.h>


@interface CategoryTableViewController : UITableViewController {
    NSArray *cats;
    UIActivityIndicatorView *activityIndicator;
}

@end

TableViewController.m

#import "CategoryTableViewController.h"
#import "NieuwsDataManager.h"
#import "CategoryTableCell.h"
#import "NiewsCategory.h"
#import "NieuwsTableViewController.h"


@implementation CategoryTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        cats = [[NSArray alloc ] initWithArray:[NieuwsDataManager sharedInstance].newsCategories];
        self.title = @"Nieuws" ;
        activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame: CGRectMake(100,150,120,120)];
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;


        [self.view addSubview:activityIndicator];


    }
    return self;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [activityIndicator startAnimating];



    NiewsCategory *cat = [[NieuwsDataManager sharedInstance].newsData objectForKey:[cats objectAtIndex:indexPath.row]];



    [[NieuwsDataManager sharedInstance] getNewsByCategory:cat];



    NieuwsTableViewController *nt = [[NieuwsTableViewController alloc] initWithCategory:cat];

    [activityIndicator stopAnimating];

    [self.navigationController pushViewController:nt animated:YES];

    [nt release];

    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

Upvotes: 0

Views: 2919

Answers (4)

viral shah
viral shah

Reputation: 93

[self.view addSubview:activityIndicator];

From this code I think it's possible that tableView is not as set as the self.view. You are adding it in the view so it might be possible that your method [activityIndicator startAnimating];is actually working, but you have to set it "bring subview to front." It's possible that it is animated underneath the tableview.

Upvotes: 2

AndreaG
AndreaG

Reputation: 1116

Is self.view already available in init method? Shouldn't such operation performed in viewDidLoad method?

Upvotes: 0

Alex Nichol
Alex Nichol

Reputation: 7510

I have noticed in the past that UIActivityIndicators do draw while the UI thread is hung, but it may be possible that you need to perform your task in a background thread in order for the UI thread to animate the progress indicator. See Apple's documentation about Concurrency Programming. In general it is good to perform lengthy operations on a background thread so that the UI thread can remain responsive to the user.

Upvotes: 0

Adam Ivancza
Adam Ivancza

Reputation: 2529

I would do this:

- (id)initWithStyle:(UITableViewStyle)style {
  //your code
  [self.view addSubview:activityIndicator];
  [activityIndicator startAnimating];
}

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidLoad];
  [activityIndicator stopAnimating];
}

And i found a tutorial about this, it shows a really nice way to do this: http://www.markbetz.net/2010/09/30/ios-diary-showing-an-activity-spinner-over-a-uitableview/

Upvotes: 1

Related Questions