'UITableView' 与 'scrollViewDidScroll' 方法中的 'UIScrollView' 相同。
因此,它很容易模拟无限滚动。
将数组加倍,以便将头部和尾部连接在一起以模拟圆桌
当用户倾向于到达表格的开头或结尾时,使用我的以下代码使用户在双倍表的第一部分和双倍表的第二部分之间切换。
:
/* To emulate infinite scrolling...
The table data was doubled to join the head and tail: (suppose table had 1,2,3,4)
1 2 3 4|1 2 3 4 (actual data doubled)
---------------
1 2 3 4 5 6 7 8 (visualising joined table in eight parts)
When the user scrolls backwards to 1/8th of the joined table, user is actually at the 1/4th of actual data, so we scroll instantly (we take user) to the 5/8th of the joined table where the cells are exactly the same.
Similarly, when user scrolls to 6/8th of the table, we will scroll back to 2/8th where the cells are same. (I'm using 6/8th when 7/8th sound more logical because 6/8th is good for small tables.)
In simple words, when user reaches 1/4th of the first half of table, we scroll to 1/4th of the second half, when he reaches 2/4th of the second half of table, we scroll to the 2/4 of first half. This is done simply by subtracting OR adding half the length of the new/joined table.
*/
-(void)scrollViewDidScroll:(UIScrollView *)scrollView_
{
CGFloat currentOffsetX = scrollView_.contentOffset.x;
CGFloat currentOffSetY = scrollView_.contentOffset.y;
CGFloat contentHeight = scrollView_.contentSize.height;
if (currentOffSetY < (contentHeight / 8.0)) {
scrollView_.contentOffset = CGPointMake(currentOffsetX,(currentOffSetY + (contentHeight/2)));
}
if (currentOffSetY > ((contentHeight * 6)/ 8.0)) {
scrollView_.contentOffset = CGPointMake(currentOffsetX,(currentOffSetY - (contentHeight/2)));
}
}
PS - 我在我的一个名为 NT Time Table (Lite) 的应用程序中使用了此代码。如果您想要预览,您可以查看应用程序:https ://itunes.apple.com/au/app/nt-time-table-lite/id528213278?mt=8
如果您的表有时可能太短,则可以在上述方法的开头添加一个 if 逻辑以在数据计数例如小于 9 时退出该方法。