-2

Want to add the pull to refresh in bottom of tableview.

First want to show the numbers in tableview 100 to 90.

Then add the pull to refresh in bottom of tableview. pull it want to display 80 to 70, then pull refresh 70 to 60, then pull refresh 50 to 40... etc final 1 to 10 means get stoped display "no data available". how to achieve this. help me thanks advance.

Here my code.

 @IBOutlet weak var dataTbl: UITableView!
    var numbers: [Any] = ["90","91","92","93","94","95","96","97","98","99","100"] // display first 100 to 90. 

Loaded in tableview.

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.numbers.count
    }



 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell:UITableViewCell = self.dataTbl.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
        cell.textLabel?.text = self.numbers[indexPath.row] as? String

        return cell
    }

searched in google for add the refresh in buttom of tableview. i got this code.

let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)
 spinner.color = UIColor.darkGray
 spinner.hidesWhenStopped = true
 tableView.tableFooterView = spinner

show spinner using

spinner.startAnimating()

hide it using

spinner.stopAnimating()

By using this code how to do pull to refresh help me.

saravanar
  • 639
  • 2
  • 11
  • 25

2 Answers2

3

Use following delegate to detect the end of the table and to add spinner logic with table footer.

func scrollViewDidScroll(_ scrollView: UIScrollView) {
            if tableView.contentOffset.y >= (tableView.contentSize.height - tableView.frame.size.height) {
                    let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)
     spinner.color = UIColor.darkGray
     spinner.hidesWhenStopped = true
     tableView.tableFooterView = spinner
            }
        }

Once you append those more entries to your data source then remove footer view

tableView.tableFooterView = nil
KavyaKavita
  • 1,581
  • 9
  • 16
  • how to refresh elements 100 to 90 , 90 to 80, 80 to 70 – saravanar Sep 08 '17 at 02:45
  • If you are fetching it from server then you need to send offset to server to fetch next 10 records, when you get response from server then append those records to the previous data source – KavyaKavita Sep 08 '17 at 04:27
1

You will need to use the below code,

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if tableView.contentOffset.y >= (tableView.contentSize.height - tableView.frame.size.height) {
            //you reached the bottom of tableview, you can append the other 10 numbers to array and do reload
    }
}

You don't need to add UIScrollViewDelegate to achieve this, i hope UITableViewDelegate is enough. As UITableview inherits the property of UIScrollView Hope this helps

Austin Michael
  • 460
  • 4
  • 18