TinMan7757
TinMan7757

Reputation: 148

UIActivityIndicator not showing over UITableView when reloading new data

My basic problem is that I cannot seem to work out how to bring the Activity indicator to the top so I can show that a process is running while new data is populating the UITableView. To load new data I am using a Segmented Control. When segment 0 is pressed it will load the first xml feed and when segment 1 is pressed it will load a second xml feed. I have tried using dispatch_queue_t etc. it does load the information and repopulate the table, it just doesn't show the indicator.

This is the code i am currently using.

if (Seg.selectedSegmentIndex == 0) {
    timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(loading) userInfo:nil repeats:YES];

    dispatch_queue_t loader = dispatch_queue_create("a", NULL);
    dispatch_async(loader, ^{

        xml = [[XmlParser alloc]init];
        url = [NSURL URLWithString:kTodaysReport];
        dict = [[NSMutableDictionary alloc]init];
        [xml loadXML:url];
        [tableView addSubview:ActInd];
        dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleUpdate:) name:@"update" object:nil];

        });
    });



}
else if (Seg.selectedSegmentIndex ==1){
   // tableView = [[UITableView alloc]init];


    timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(loading) userInfo:nil repeats:YES];

    dispatch_queue_t loader = dispatch_queue_create("a", NULL);
    dispatch_async(loader, ^{

        xml = [[XmlParser alloc]init];
        url = [NSURL URLWithString:kMonthsReport];
        dict = [[NSMutableDictionary alloc]init];
        [xml loadXML:url];
        [tableView addSubview:ActInd];
        dispatch_async(dispatch_get_main_queue(), ^{
            [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleUpdate:) name:@"update" object:nil];
        });
    });
}

Thanks in advance.

EDIT:Relevant code

- (void)viewDidLoad
{
[super viewDidLoad];

dispatch_queue_t loader = dispatch_queue_create("a", NULL);
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(loading)     userInfo:nil repeats:YES];


    dispatch_async(loader, ^{


    xml = [[XmlParser alloc]init];
    url = [NSURL URLWithString:kTodaysReport];
    dict = [[NSMutableDictionary alloc]init];
    [xml loadXML:url];
    [UiTableView addSubview:ActInd];
    dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleUpdate:)     name:@"update" object:nil];
        [ActInd stopAnimating];
        [UiTableView reloadData];
    });
    });

}

-(void)handleUpdate:(NSNotification*)notification{
mainArray = [xml parsedArray];
[UiTableView reloadData];
}

-(void)loading{
if (![xml val]) {
    [ActInd startAnimating];
}else
{
    [ActInd stopAnimating];
    [UiTableView reloadData];
}
}
-(IBAction)segvalueChange:(id)sender{

if (Seg.selectedSegmentIndex == 0) {
    timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(loading) userInfo:nil repeats:YES];

    dispatch_queue_t loader = dispatch_queue_create("a", NULL);
    dispatch_async(loader, ^{

        xml = [[XmlParser alloc]init];
        url = [NSURL URLWithString:kTodaysReport];
        dict = [[NSMutableDictionary alloc]init];
        [xml loadXML:url];
        [UiTableView addSubview:ActInd];
        dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleUpdate:) name:@"update" object:nil];

        });
    });



}
else if (Seg.selectedSegmentIndex ==1){
   // UiTableView = [[UITableView alloc]init];


    timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self     selector:@selector(loading) userInfo:nil repeats:YES];

    dispatch_queue_t loader = dispatch_queue_create("a", NULL);
    dispatch_async(loader, ^{

        xml = [[XmlParser alloc]init];
        url = [NSURL URLWithString:kMonthsReport];
        dict = [[NSMutableDictionary alloc]init];
        [xml loadXML:url];
        [UiTableView addSubview:ActInd];
        dispatch_async(dispatch_get_main_queue(), ^{
            [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleUpdate:) name:@"update" object:nil];
        });
    });
}
else{
}
}

Upvotes: 0

Views: 100

Answers (1)

Woodstock
Woodstock

Reputation: 22966

The problem is that you are interacting with the UI on a background thread. Move all UI related code (including that which interacts with the activity view) into the GCD block for the main thread.

Upvotes: 1

Related Questions