UITableView with custom paging behavior

I am trying to implement paging for custom size pages in a UITableView. I'm trying to make sure that the top of the active cell matches the top of the View table, while at the same time showing the top of the next cell at the bottom of the View table (to tilt the user to scroll and view more cells).

My cells are of equal height.

If I install paging=YES, this leads to a slight offset, which increases when I view the pages. This is because my tableView is slightly higher than one cell, and the cell height / page size does not align.

I tried different things with paging enabled. I tried to set the size of the View table to the height of the cell, but then turned off clipping and masking so that the user could still see the next cell. This does not work, since the next cell is added only to the base scrollView in the very last ms before the cell scrolls to the bounding box of the tableView.

Then I started to implement various scrollView delegate methods to mimic the paging behavior - I cannot figure out what is right.

I, by the way, tried something like this:

- (void) scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    float cellHeight = [myCell rowHeight];
    int index = floorf(scrollView.contentOffset.y / cellHeight);

    *targetContentOffset = CGPointMake(targetContentOffset->x, targetContentOffset->y = (index * cellHeight));
}

While this seems like the right thing, it doesn't behave like scrollView / tableView with paging enabled.

I found several posts here from people trying to achieve the same, but the answers suffer from the same β€œlaid-back feeling” that everything I tried does.

.

iOS >= 5.0

+5
3

scrollViewWillEndDragging:withVelocity:targetContentOffset:, , targetContentOffset, . jjv360 , , ( jjv360 ).

, UITableView , paged UIScrollView, decelerationRate ( init/viewDidLoad/ ).

self.tableView.decelerationRate = UIScrollViewDecelerationRateFast;
+11

, UIScrollView:

-(void)scrollViewWillEndDragging:(UIScrollView*)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint*)targetContentOffset {

    // Get index path for target row
    NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint:(*targetContentOffset)];

    // Set new target
    (*targetContentOffset) = [self.tableView rectForRowAtIndexPath:indexPath].origin;

}
+3

I solved this problem with this code:

- (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView {

    CGFloat pageWidth = self.collectionView.frame.size.width + 10 /* Optional Photo app like gap between images */;

    _currentPage = floor((self.collectionView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;

    NSLog(@"Dragging - You are now on page %i", _currentPage);
}

-(void) scrollViewWillEndDragging:(UIScrollView*)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint*)targetContentOffset {

    CGFloat pageWidth = self.collectionView.frame.size.width + 10;

    int newPage = _currentPage;

    if (velocity.x == 0) // slow dragging not lifting finger
    {
        newPage = floor((targetContentOffset->x - pageWidth / 2) / pageWidth) + 1;
    }
    else
    {
        newPage = velocity.x > 0 ? _currentPage + 1 : _currentPage - 1;

        if (newPage < 0)
            newPage = 0;
        if (newPage > self.collectionView.contentSize.width / pageWidth)
            newPage = ceil(self.collectionView.contentSize.width / pageWidth) - 1.0;
    }

    NSLog(@"Dragging - You will be on %i page (from page %i)", newPage, _currentPage);

    *targetContentOffset = CGPointMake(newPage * pageWidth, targetContentOffset->y);
}

Thanks to http://www.mysamplecode.com/2012/12/ios-scrollview-example-with-paging.html for the correct indication.

+1
source

All Articles