如何在tableview上实现循环滚动。

| 您能帮我在表格视图中进行圆形滚动吗? 我希望如果向下滚动tableview,则各行应以相反的方式前进- 看起来应该向后移动(底部行又从顶部向下移动),即基本上是循环滚动。 我该怎么做。有任何建议请。 提前感谢。     
已邀请:
您可以“伪造”循环滚动,再次重复相同的单元格。在numberOfRowsInSection方法中,返回实际行数的n倍。确保n足够大。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return numberOfActualRows*100;
}
然后在cellForRowAtIndexPath方法(及其他地方)中,使用mod运算符(%)返回正确的单元格。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger actualRow = indexPath.row % numberOfActualRows;
    ...
}
您可能要隐藏滚动指示器。
self.tableView.showsVerticalScrollIndicator = NO;
在显示表格之前,您可能还希望将表格视图放到中间,这样向后滚动即可正常工作。
[self.tableView scrollToRowAtIndexPath: [NSIndexPath indexPathForRow:[self tableView:self.tableView numberOfRowsInSection:0]/2 inSection:0]  atScrollPosition:UITableViewScrollPositionTop animated:NO];
当然,如果用户不断滚动,最终将击中底部或顶部。     
已经问过这个问题:实现循环UITableView 我在此处复制该答案以使其更容易,因为询问者没有在我的答案中打勾。 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.

 Written and posted by Anup Kattel. Feel free to use this code. Please keep these comments if you don\'t mind.
*/


-(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)));
    }

}
附言-我在我的一个名为NT时间表(Lite)的应用程序中使用了此代码。如果要预览,可以签出该应用程序:https://itunes.apple.com/au/app/nt-time-table-lite/id528213278?mt=8 如果您的表有时可能太短,则可以在上述方法的开头添加if逻辑,以在数据计数小于9时退出。     
我自己还没有做过,但是您可以尝试与UIScrollView一起使用的方法来实现视图的循环滚动(毕竟UITableView是UIScrollView的子类)。 我将执行以下操作: 创建具有任意数量的单元格的UITableView(至少7个单元格,但需要更多单元格以防止最后快速滚动碰撞) 定位您的UITableView,以便显示中心单元格 保持指向要显示的单元格索引的指针 在cellForRowAtIndexPath中:使用指针作为偏移量,并向其添加行以获取所需的单元格 当UITableView停止移动时(您的UITableViewDelegate可以用作UIScrollViewDelegate,因此您可以使用scrollViewDidEndDecelerating)。将偏移索引设置为当前单元格,将表格视图移回没有动画的中心单元格,然后重新加载数据。 您将遇到的问题是,如果用户不停地滚动而不停滚动,则最终他们会在达到表中的单元格数量时碰到保险杠。 希望这会有所帮助,如果可以使用,并且一切正常,请回发。 问候 戴夫     
这很难实现。但是,请看一下ScorllingMadness,它在滚动视图中显示了嵌套(循环)页面的演示。 您需要在此处使用类似的技巧,因为UITableView是UIScrollView的子类。     

要回复问题请先登录注册