Search code examples
iosuitableviewswiftnsindexpath

Center Cell UITableView When Scrolling Is Finished


I have a UITableView, and I don't want it to stop between two cells when it stops after scrolling. So I need to move the cell which is the nearest of the Y center of the screen, when the velocity of the UITableView is less than a given value.

I'm sure I'm not the first who wants this effect, but I didn't find anything in my research. There must be a word for this that I don't know. Does someone have a link or something ?

I tried to do this on my own, but I don't find how to get the cell. I used tableView.indexPathsForVisibleRows(), so I have an array of indexPaths, but now I don't know how to call the corresponding cell. There is cellForRowAtIndexPath, but this method creates a new cell, doesn't it ?


Solution

  • You need to implement -scrollViewWillEndDragging:withVelocity:targetContentOffset: in your table view delegate, just as in this other question here.

    Basically, this method allows you to tweak the position where the velocity scroll will halt.

    I can't help you more with code without knowing the structure of your table view (cells of variable height?).