Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want UITableView to "snap to cell"

Tags:

I am displaying fairly large images in a UITableView. As the user scrolls, I'd like to the table view to always snap the center-most photo in the middle. That is, when the table is in a resting state, it will always have a UITableViewCell snapped to the center.

How does one do this?

like image 256
VaporwareWolf Avatar asked Apr 18 '13 16:04

VaporwareWolf


1 Answers

You can use the UIScrollViewDelegate methods on UITableView to do this:

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {     // if decelerating, let scrollViewDidEndDecelerating: handle it     if (decelerate == NO) {         [self centerTable];     } }  - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {     [self centerTable]; }  - (void)centerTable {     NSIndexPath *pathForCenterCell = [self.tableView indexPathForRowAtPoint:CGPointMake(CGRectGetMidX(self.tableView.bounds), CGRectGetMidY(self.tableView.bounds))];      [self.tableView scrollToRowAtIndexPath:pathForCenterCell atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; } 
like image 61
jszumski Avatar answered Sep 18 '22 14:09

jszumski