3

Is there any way to avoid the tableview keep scrolling when reaching the bottom cell?

So far I managed to do this:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if indexPath.row == dataArray.count - 1 {
        tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
    }
}

and that allows me only to stop the scroll when the user "rolled" the table to bottom. But after that the user can scroll again. Only if I could disable the "down" scrolling I could achieve it. Enabling it again as soon as it moved.

But it seems a lot of work just to do that, maybe I'm missing some property that fixes the last cell bottom to tableview's bottom.

Any ideas?

3 Answers3

0

There are 3 things you need to do.

tableView.footerView = UIView()
tableView.bounces = false

And after the tableView has been reloaded. tableViewHeight(your constraint).constant = tableView.contentSize.height

This will cause the tableView to only display the populated cells, you will restrict the height to be as high as the content and you can't bounce the content as you want it to stop at last cell.

Vollan
  • 1,887
  • 11
  • 26
0

Remember that an UITableView is a subclass of UIScrollView

Starting here, you have 2 options :

1 - Disable the bounces effect : tableView.bounces = false

2 - Prevent scroll further than the bottom of your tableView :

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.y + scrollView.bounds.size.height > scrollView.contentSize.height {
    scrollView.contentOffset.y =  scrollView.contentSize.height -  scrollView.bounds.size.height
    }
}
CZ54
  • 5,488
  • 1
  • 24
  • 39
0

You can do by disabling it directly from Storyboard:

enter image description here

Disable the Bounce on scroll to Untick

Also if u want to disable the lines coming at bottom when data is less you can use:

tableView.footerView = UIView()
Abhirajsinh Thakore
  • 1,806
  • 2
  • 13
  • 23
  • Tried this and I still have a blank space at the bottom. –  Apr 11 '18 at 07:46
  • i did not get. i mean you are facing problem in scrolling or having issue in seeing lines at bottom.? – Abhirajsinh Thakore Apr 11 '18 at 07:47
  • I guess it's both . I needed the table to stop scrolling so the last cells bottom == table bottom. Obviously i have to lost bounce for that. The problem now it's i keep seeing the empty space (and also a new empty space on heather) –  Apr 11 '18 at 07:49
  • Can you please edit your question and show us how you implemented Table view. So that i can look into that – Abhirajsinh Thakore Apr 11 '18 at 07:57