【问题标题】:Creating a UICollectionView with infinite vertical scrolling cells创建具有无限垂直滚动单元格的 UICollectionView
【发布时间】:2014-08-19 21:52:18
【问题描述】:

下面的代码从我的集合视图中调用并打印出 50 个单元格,从标签计数 0 开始到计数 49。

- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 50;
}

- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

    cell.backgroundColor = [UIColor whiteColor];

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 40.0)];
    view.backgroundColor = [UIColor blueColor];

    NSInteger i = indexPath.row;
    NSString *string = [[NSNumber numberWithInteger:i] stringValue];

    UILabel *infoLabel = [ [UILabel alloc ] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 40.0) ];
    infoLabel.textAlignment =  NSTextAlignmentLeft;
    infoLabel.textColor = [UIColor whiteColor];
    infoLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:(12.0)];
    infoLabel.text = string;
    [view addSubview:infoLabel];

    [cell addSubview:view];



    return cell;
}

当我滚动时计数达到 40(屏幕上可见 40)时,我该怎么做,然后我想再加载 50 个单元格并继续从 50 计数到 99 等等?

【问题讨论】:

  • 我想垂直滚动并为每个项目获取新图像(稍后)

标签: ios ios7 uicollectionview uicollectionviewcell infinite-scroll


【解决方案1】:

这样设置阈值

self.threshold = self.dataSource.count - 10;

然后在scrollViewDidScroll:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSArray * indexPaths = [self.collectionView indexPathsForVisibleItems];

    for (NSIndexPath * ip in indexPaths)
    {
        if (ip.row > self.threshold)
        {
            // load other 50 pages
            [self.collectionView reloadItemsAtIndexPaths:[self.collectionView indexPathsForVisibleItems]];
            [self.collectionView reloadData];

            // update the threshold
            self.threshold += 50;
        }
    }
}

【讨论】:

  • 我看到它被正确调用了,但是在我的问题中,我使用上面的什么代码来重新加载接下来的 50 个部分?
  • 添加了这个:[self.collectionView reloadItemsAtIndexPaths:[self.collectionView indexPathsForVisibleItems]]; [self.collectionView reloadData];
  • 如果使用这种方法,是否必须更改 numberOfItemsInSection 才能添加阈值?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-09
  • 2022-12-24
  • 1970-01-01
  • 2015-02-11
  • 2019-10-04
  • 1970-01-01
  • 2016-07-24
相关资源
最近更新 更多