1

So what I am trying to achieve in my app is have a UITableView that when the user scrolls, snaps to the closest cell there is. So if I have 3 cells, and the user scrolls, if the user is closest to the top of the second cell, snap the UITableView there.

Hopefully I have conveyed the feature I am trying to achieve. I have done some basic research, and have found this method, scrollViewWillEndDragging:withVelocity:targetContentOffset:, but I am not familiar with this method and how it works.

Any help?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • Possible duplicate of [Want UITableView to "snap to cell"](https://stackoverflow.com/questions/16088615/want-uitableview-to-snap-to-cell) – gog Oct 26 '18 at 14:00

1 Answers1

2

if your cells are all a fixed height, just take a look at the targetContentOffset.y, and then round it to your nearest cell height:

targetContentOffset->y = floorf(targetContentOffset->y / self.rowHeight) * self.rowHeight
Ben Gottlieb
  • 85,404
  • 22
  • 176
  • 172
  • So just implementing scrollViewWillEndDragging:withVelocity:targetContentOffset: and that line should do the trick? –  Jun 20 '14 at 21:19
  • not sure host the rest of your code is laid out, but if there's nothing funky going on, that's it. – Ben Gottlieb Jun 20 '14 at 21:23
  • 1
    And what if they are all different heights? –  Jun 20 '14 at 23:33