iOS TableView with paging?

12,998

Setting the pagingEnabled property of your UITableView to YES is always an option… However, default UIScrollView paging will automatically page in multiples of your UITableView's frame height (unlikely the same height of your UITableViewCells), so you'll probably need to implement scrollViewWillEndDragging:withVelocity:targetContentOffset. This method is called when a UIScrollView (or UITableView) begins to decelerate, and it allows us to specify where the UIScrollView should finish movement.

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView 
                    withVelocity:(CGPoint)velocity 
             targetContentOffset:(inout CGPoint*)targetContentOffset
{
    //Intercept and recalculate the desired content offset
    CGPoint targetOffset = [self recalculateUsingTargetContentOffset:targetContentOffset];

    //Reset the targetContentOffset with your recalculated value
    targetContentOffset->y = targetOffset.y;
}

You may want to check out this post (UITableView w/ paging & momentum) in order to get a feel for how you'll need to tailor the target content offset recalculation method to fit your needs.

Share:
12,998
AttilaTheFun
Author by

AttilaTheFun

Updated on June 15, 2022

Comments

  • AttilaTheFun
    AttilaTheFun almost 2 years

    I am working on an iOS app with a nice UI for viewing users’ friends which is currently in a vertical ScrollView with paging enabled and 100×100pt pictures. However, with 500+ friends, this is insanely inefficient and slow, loading all of those pictures in memory at once. What I’d like to do is use a TableView and load the images when I load the cells, but I really like the snapping effect of having a ScrollView with paging enabled.

    How would you recommend I add this functionality to a TableView?

  • Pathak Ayush
    Pathak Ayush over 4 years
    can anyone translate it in swift?