Detect when UITableView has scrolled to the bottom
Swift 3
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row + 1 == yourArray.count {
print("do something")
}
}
How to know when UITableView did scroll to bottom in iPhone?
The best way is to test a point at the bottom of the screen and use this method call when ever the user scrolls (scrollViewDidScroll
):
- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point
Test a point near the bottom of the screen, and then using the indexPath it returns check if that indexPath is the last row then if it is, add rows.
How to determine if the user has scrolled to the bottom of the UITableView?
UITableView inherits from UIScrollView, and scroll view exposes a contentOffset
property (documentation here).
Use this with a bit of math to determine if the contentOffset is within frame.size.height
of the bottom.
Update: here's a stab at a formula that will give you what you want:
if(tableView.contentOffset.y >= (tableView.contentSize.height - tableView.frame.size.height)) {
//user has scrolled to the bottom
}
Detect when UITableView section has scrolled out of view
You can use ...didEndDisplayingCell...
function in delegate:
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.section == 0 && indexPath.row == lastRowInFirstSection {
// first section is out
}
}
Note that this function is called once each cell went out from the top or bottom of the screen, so you need to check the indexPath
to make sure that was the cell you need.
Also you can check if the second section is visible to detect if first section is going out from the bottom if you needed:
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.section == 0 && indexPath.row == lastRowInFirstSection {
if tableView.indexPathsForVisibleRows?.contains (where: { $0.section == 0 }) == true {
// It goes out from bottom. So we have to check for the first cell if needed
} else {
// It goes out from top. So entire section is out.
}
}
}
How to scroll to the bottom of a UITableView on the iPhone before the view appears
I believe that calling
tableView.setContentOffset(CGPoint(x: 0, y: CGFloat.greatestFiniteMagnitude), animated: false)
will do what you want.
Related Topics
How to Simultaneously Satisfy Constraints, Will Attempt to Recover by Breaking Constraint
Hide Separator Line on One Uitableviewcell
How to Use Uivisualeffectview to Blur Image
Uinavigationbar Custom Back Button Without Title
Sharing Data in Between Apps in Ios
iOS 7 Tableview Like in Settings App on iPad
Disable Uiscrollview Scrolling When Uitextfield Becomes First Responder
Uisegmentedcontrol Below Uinavigationbar in iOS 7
Swift: Issue in Converting String to Double
Changing Tint/Background Color of Uitabbar
How to Create a Guid/Uuid Using Ios
How to Validate an E-Mail Address in Swift
Uicollectionview Inside a Uitableviewcell - Dynamic Height
Run Code Only After Asynchronous Function Finishes Executing
Draw Segments from a Circle or Donut