Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when UITableView has scrolled to the bottom

Is the following post still the accepted way of detecting when an instance of UITableView has scrolled to the bottom [in Swift], or has it been altered (as in: improved) since?

Problem detecting if UITableView has scrolled to the bottom

Thank you.

like image 793
Joseph Beuys' Mum Avatar asked Aug 18 '16 09:08

Joseph Beuys' Mum


People also ask

How will you know the end of loading of UITableView?

willDisplayCell: used here for smoother UI (single cell usually displays fast after willDisplay: call). You could also try it with tableView:didEndDisplayingCell: . Much better for knowing when all the cells load that are visible. However this will be called whenever the user scrolls to view more cells.

How can I tell if a tableView is scrolling?

Since a scrollView has a panGesture we can check the velocity of that gesture. If the tableView was programmatically scrolled the velocity in both x and y directions is 0.0. By checking this velocity we can determine if the user scrolled the tableView because the panGesture has a velocity.

Is UITableView scrollable?

UITableView scrolls back because it's content size is equal to it's frame (or near to it). If you want to scroll it without returning you need add more cells: table view content size will be large then it's frame.


1 Answers

try this

 func scrollViewDidScroll(_ scrollView: UIScrollView) {     let height = scrollView.frame.size.height     let contentYoffset = scrollView.contentOffset.y     let distanceFromBottom = scrollView.contentSize.height - contentYoffset     if distanceFromBottom < height {         print(" you reached end of the table")     } } 

or you can find in this way

if tableView.contentOffset.y >= (tableView.contentSize.height - tableView.frame.size.height) {         //you reached end of the table } 
like image 52
Anbu.Karthik Avatar answered Oct 17 '22 20:10

Anbu.Karthik