Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a ActivityIndicator to the bottom of UITableView while loading

Tags:

I want to add a ActivityIndicator to the bottom of my UITableView when the last cell is displayed, while I'm fetching more data, then when the data got fetched hide it.

So I scroll to the bottom -> last row displayed -> spinner starts spinning while data is fetched -> Data fetched, hide the spinner -> new data added to the tableview.

Any tips on how can I achieve this?

Thanks ;)

like image 774
Ivan Cantarino Avatar asked Feb 25 '17 14:02

Ivan Cantarino


People also ask

How do I add a search bar to UITableView?

Adding Search Bar on TableView Add the UISearchBar at the top of your UITableView. … and give the name searchBar. In the ViewController add a Boolean called searching to know when to switch between the full list of items and the list of items from the searching results.

Is it possible to add UITableView within a UITableViewCell?

yes it is possible, I added the UITableVIew within the UITableView cell .. :) no need to add tableview cell in xib file - just subclass the UITableviewCell and use the code below, a cell will be created programatically.

How do I populate UITableView?

There are two main base ways to populate a tableview. The more popular is through Interface Building, using a prototype cell UI object. The other is strictly through code when you don't need a prototype cell from Interface Builder.


1 Answers

Add This Function

 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: .gray)         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     } } 

and tableFooterView should hide when data load.

like image 132
AyAz Avatar answered Sep 21 '22 20:09

AyAz