我在 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/