gpt4 book ai didi

ios - 创建具有无限垂直滚动单元格的 UICollectionView

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:00:36 24 4
gpt4 key购买 nike

下面的代码从我的 Collection View 中调用并打印出 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 等等?

最佳答案

像这样设置一个阈值

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

关于ios - 创建具有无限垂直滚动单元格的 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24474653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com