poq
poq

Reputation: 11

How can I detect the bottom of tableview with lots of sections? (in swift)

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

Answers (4)

Fansad PP
Fansad PP

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

Andr&#233; Slotta
Andr&#233; Slotta

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

kirander
kirander

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

Mario
Mario

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

Related Questions