Reputation: 11
I want to know how can I detect when I scroll down at the bottom of the tableView cells which have lots of sections in swift4.
I know I can detect it with using this function,
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {}
but it only works when there is 1 section.
Please let me know how to detect the bottom of the tableview cell when there are lots of sections.
Upvotes: 0
Views: 3923
Reputation: 469
you can find that the tableview is scrolled to the bottom by validating scrollViewDidScroll delegate of your tableview...And validate is that the last section in the array
extension YourViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if someFlag == SectionArray.count - 1
{
if scrollView.contentSize.height - scrollView.contentOffset.y - scrollView.frame.height < 100 {
// only 100 pt are left from the bottom
}
}
}
}
Upvotes: 0
Reputation: 14040
Depending on what exactly you want to check you could do something like this:
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.section == tableView.numberOfSections - 1 {
print("will display last section")
if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 {
print("will display last row")
}
}
}
Upvotes: 0
Reputation: 2256
You can get that info in two ways. One way is to ask your model and compare to given indexPath section and row to check that it is the last section/cell. Second, is to ask tableView delegate methods to get the same data as from the model (cause tableView is in sync with the model).
Upvotes: 0
Reputation: 459
You can do that by using UIScrollViewDelegate
and checking the contentOffset
extension YourViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentSize.height - scrollView.contentOffset.y - scrollView.frame.height < 100 {
// only 100 pt are left from the bottom
}
}
}
Upvotes: 5