Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>UTTableView</code> is a subclass of <code>UIScrollView</code>, so you can use <code>scrollViewDidScroll</code> method to rearrange your TableView scroll position after scroll.</p> <p>You can use <code>tableView.contentOffset</code> to get where is your current scroll position. And by dividing it to a meaningful number you can get which cell is on top.</p> <pre><code>int cellIndex = tableView.contentOffset / cellHegiht; </code></pre> <p>Or you can get currently visible cell on center (top, bottom) like this:</p> <pre><code>NSArray *indexPathsForVisibleRows = [tableView indexPathsForVisibleRows]; NSIndexPath *selectedIndexPath = [indexPathsForVisibleRows objectAtIndex:(indexPathsForVisibleRows.count / 2)]; //this gets center cell </code></pre> <p>After calculating which cell should be clicked by its edge on top (or bottom) you can correct the drift from cell edge by calling:</p> <pre><code>[tableView scrollToRowAtIndexPath:selectedIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; </code></pre> <p>You can use <code>UIScrollViewDelegate</code> methods to activate snapping. You can activate either when the scrolling animation completed or while the animation still continues. This will generate different user interaction and can't say one is better then another.</p> <p>But just implementing following method and nothing else looks like going to be my favorite:</p> <pre><code>- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { int cellHeight = 41; *targetContentOffset = CGPointMake(targetContentOffset-&gt;x, targetContentOffset-&gt;y - (((int)targetContentOffset-&gt;y) % cellHeight)); } </code></pre> <p>This method gets called when user touches up on <code>TableView</code> to notify application. <code>targetContentOffset</code> is an <code>inout</code> variable so you can actually set final scroll position while animation is continuing. Using right <code>cellHeight</code>, your <code>TableView</code> will always snap to cells.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload