使用无限垂直滚动单元格创建一个UICollectionView

3
以下代码从我的Collection View中调用,并输出从标签0到标签49的50个单元格。
- (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个元素时,加载50个更多的元素,并继续从50到99进行计数?


我想要垂直滚动,并为每个项目获取新的图片(稍后)。 - cdub
1个回答

3

设定类似于这样的阈值

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个部分? - cdub
添加以下代码:[self.collectionView reloadItemsAtIndexPaths:[self.collectionView indexPathsForVisibleItems]]; [self.collectionView reloadData]; - cdub
如果使用这种方法,您是否必须更改numberOfItemsInSection以添加阈值? - SAHM

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接