What is a good way to display a spinner at the bottom of a tableview when a user scrolls, but the tableview cells load slowly?

Viewed 2099

I have a UITableView that displays some content. When the user scrolls upward, the cells below don't always load immediately. This creates an area of white space at the bottom of the table. I would like to display a spinner in the white space and have it disappear when the content is done loading, but I'm not sure how to go about doing this. What is a good way to implement something like this in swift?

I am new to coding and iOS, please forgive me if the question is vague or the answer is obvious.

Sample Screenshot :

sample image for desired behavior

2 Answers

Set UIActivityIndicatorView as a UITableView's footerView in viewDidLoad.

self.indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
self.indicator.hidesWhenStopped = YES;
self.indicator.frame = CGRectMake(0, 0, self.tableView.bounds.size.width, 44);

self.tableView.tableFooterView = self.indicator;

When the tableview is about to display the last row of cells and you have more data to load, then load more data.

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == self.arrList.count-1 && self.hasMoreList == YES) {
        [self loadData];
    }
}

Just start animating indicator before loading and stop animating it after loading data.

- (void)loadData {

    [self.indicator startAnimating];

    // load data and set hasMoreData here ... 

    [self.indicator stopAnimating];
}
Related