ios - 使 UITableView 像 UIPickerView 一样滚动

标签 ios uitableview uiscrollview infinite-loop

我在 UIViewController 上有一个 UITableView。 这张表有“说”5个可见类别可供选择。但是在包含内容的数组中,我有“说” 10 个类别。我现在创建的是一个普通的滚动表,它从索引 0 上的类别 1 到索引 9 上的类别 10。 但我更希望在类别 10 之后还有类别 1,反之亦然。

所以基本上是一个静态数组的无限循环。

我已经尝试使用 scrollViewDidScroll: 方法进行此操作,但是当我这样做时,它不会像您期望的 UITableView 滚动那​​样滚动。它跑到一个随机点,移动一两个类别是不可能的。

这是我的代码示例。希望有人能帮忙。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == categoryView){
        if (categoryView.contentOffset.y > 0.0) {
            loopcounter++;
            NSLog(@"ScrollView Scrolled DOWN");
            [categorytablecontent insertObject:[categorytablecontent objectAtIndex:0] atIndex:[categorytablecontent count]-1];
            [categorytablecontent removeObjectAtIndex:0];
            [categoryView reloadData];
        }
        if (categoryView.contentOffset.y < 0.0) {
            loopcounter = [categorytablecontent count];
            NSLog(@"ScrollView Scrolled UP");
            [categorytablecontent insertObject:[categorytablecontent objectAtIndex:[categorytablecontent count]-1] atIndex:0];
            [categorytablecontent removeObjectAtIndex:[categorytablecontent count]-1];
            [categoryView reloadData];
        }

        a = categoryView.visibleCells;

        for (UITableViewCell *cell in a){
            NSLog(@"Current Visible cell: %@", cell.textLabel.text);
        }
        NSLog(@"Current offset: %@", categoryView.contentOffset.y);
    }
}

最佳答案

执行此操作的好方法不止一种。您的 Controller 是否实现了UITableViewDataSource 协议(protocol)?如果是这样,最简单的方法可能就是在 cellForRowAtIndexPath 中返回数据集大小的模数。

类似于:

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    // Use appropriate fields for your categorytablecontent object here...
    cell.textLabel.text = [categorytablecontent objectAtIndex:(indexPath.row % [categorytablecontent count])];

    return cell;
}

这应该会产生一个很好的平滑无限滚动,循环遍历您的静态(或动态)内容。

关于ios - 使 UITableView 像 UIPickerView 一样滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11028421/

相关文章:

ios - 每次笔划更改用户文本字段输入并获取更改的消息

C - 锯齿波的傅立叶变换级数

iphone - 如何在 UIViewController 上没有 NavigationController 的情况下推送 DetailView

iphone - UIScrollView 中的 iOS 5 UIButtons 不可点击

uitableview - 我如何使用 swift 填充我放置在 UITableCell 中的整个 UIScrollView

iphone - iPhone 和 iPad 中的缓存目录有多大?

android - 如何使用singleWhere .. flutter访问列表内的项目

ios - 每秒重新加载 Tableview 部分会导致闪烁

ios - 自动布局 : What creates constraints named UIView-Encapsulated-Layout-Width & Height?

iphone - 缩放时带有 UIImageView 的 UIScrollView 居中旋转?