-3

I applied CircularSpinner loader to my tableview list. How can I hide the loader if the cell has been loaded? Now the loader will be appear for each access.

Sample code as below:-

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [CircularSpinner show:@"Loading" animated: TRUE type:CircularSpinnerTypeDeterminate showDismissButton:[NSNumber numberWithBool:TRUE] delegate:self];
    [CircularSpinner setValue:0.4 animated: TRUE];
}

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){
        [CircularSpinner setValue:1.0 animated: TRUE];
    }
}

EDITED:-

Here is function that I called API and setUpData will be called in ViewDidLoad.

-(void)setUpData{
    [self.manager GET:@"http://api.XXX.com/api/announcement" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {

        _serverDataArr = responseObject;

        self.dataArr=[NSMutableArray array];

        for (NSDictionary *subDic in self.serverDataArr) {

            Announcement_Model *model=[[Announcement_Model alloc]initWithDic:subDic];
            [self.dataArr addObject:model];

        }

        _rowArr=[Events_DataHelper getFriendListDataBy:self.dataArr];
        _sectionArr=[Events_DataHelper getFriendListSectionBy:[_rowArr mutableCopy]];


            [self.tableView reloadData];

        } failure:^(NSURLSessionTask *operation, NSError *error) {
            // [CircularSpinner setValue:1.0 animated: TRUE];
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Please try again"
                                                                             message:[error localizedDescription]
                                                                      preferredStyle:UIAlertControllerStyleAlert];

            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok"
                                                               style:UIAlertActionStyleCancel
                                                             handler:nil];

            [alertVC addAction:okAction];

            [self presentViewController:alertVC animated:YES completion:nil];
        }];

    }
Test 87
  • 101
  • 2
  • 10

2 Answers2

1

You can achieve it by implementing completionHandler.

For example:

func fetchApi(completion: (Bool)->()) {

    // Getting the data request......
    completion(true)
}

Call your function like this :-

fetchApi(completed: Bool) {
  if completed {
     // Hide your spinner
  }
}

I believe you can do the same in ObjC. Hope this will help.

Nizzam
  • 1,030
  • 3
  • 16
  • 29
  • thanks for sharing. Do you mind to share with Obj-C version solution? and for fetchApi (completed:bool)...call this function in viewdidload? – Test 87 Apr 11 '18 at 01:46
  • You can refer answer at [here](https://stackoverflow.com/a/39609941/5561496). Hope this will help. – Nizzam Apr 11 '18 at 03:31
0

Add CircularSpinner.hide() as I marked in the below code.

Hoping self.manager calls the API func and gives you response in the closure here.

-(void)setUpData{
    [self.manager GET:@"http://api.XXX.com/api/announcement" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
        [CircularSpinner hide]; // <- (1)
        _serverDataArr = responseObject;

        self.dataArr=[NSMutableArray array];

        for (NSDictionary *subDic in self.serverDataArr) {

        Announcement_Model *model=[[Announcement_Model alloc]initWithDic:subDic];
        [self.dataArr addObject:model];

        }

        _rowArr=[Events_DataHelper getFriendListDataBy:self.dataArr];
        _sectionArr=[Events_DataHelper getFriendListSectionBy:[_rowArr mutableCopy]];


        [self.tableView reloadData];

        } failure:^(NSURLSessionTask *operation, NSError *error) {
        // [CircularSpinner setValue:1.0 animated: TRUE];

        [CircularSpinner hide]; // <- (2)

        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Please try again"
        message:[error localizedDescription]
        preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok"
        style:UIAlertActionStyleCancel
        handler:nil];

        [alertVC addAction:okAction];

        [self presentViewController:alertVC animated:YES completion:nil];
        }];

}
dahiya_boy
  • 9,298
  • 1
  • 30
  • 51