UITableView page size when paging enabled

后端 未结 5 1800
攒了一身酷
攒了一身酷 2021-01-31 12:10

I\'m facing with a simple but tedious problem. What I\'m trying to do is make an UITableView to page like an UIScrollView but enabling paging doesn\'t help me so much because I

5条回答
  •  忘掉有多难
    2021-01-31 12:28

    Starting with k06a's answer, I've refined it a bit so it works more like the real paginated UITableView. The differences in behaviour are quite noticeable with full screen table rows. Even a mini-flick in either direction should scroll the table to the next page: I do this by checking velocity first.

    - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
                         withVelocity:(CGPoint)velocity
                  targetContentOffset:(inout CGPoint *)targetContentOffset
    {    
        CGFloat rowHeight = tableView.rowHeight;
        int verticalOffset = ((int)targetContentOffset->y % (int)rowHeight);
        if (velocity.y < 0)
        {
            targetContentOffset->y -= verticalOffset;
        }
        else if (velocity.y > 0)
        {
            targetContentOffset->y += (rowHeight - verticalOffset);
        }
        // No velocity, snap to closest page
        else
        {
            if (verticalOffset < rowHeight / 2)
            {
                targetContentOffset->y -= verticalOffset;
            }
            else
            {
                targetContentOffset->y += (rowHeight - verticalOffset);
            }
        }    
    }
    

    Note that additionally setting

     self.tableView.decelerationRate = UIScrollViewDecelerationRateFast;
    

    in viewDidLoad: makes it closer to the real thing, but not quite.

    I've been fiddling with setting even faster deceleration rates using the code shown here but I couldn't get it right.

提交回复
热议问题