Reputation: 1013
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 :
Upvotes: 4
Views: 2700
Reputation: 56
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];
}
Upvotes: 1
Reputation: 1531
I think it's helpful for you..
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let lastSectionIndex = tableView.numberOfSections - 1
let lastRowIndex = tableView.numberOfRows(inSection: lastSectionIndex) - 1
if indexPath.section == lastSectionIndex && indexPath.row == lastRowIndex {
// print("this is the last cell")
let spinner = UIActivityIndicatorView(activityIndicatorStyle: .red)
spinner.startAnimating()
spinner.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: tableView.bounds.width, height: CGFloat(44))
self.tableview.tableFooterView = spinner
self.tableview.tableFooterView?.isHidden = false
}
}
tableFooterView
should be hide when data load.
when above function isn't work so you can prefer this link.
Upvotes: 4