gpt4 book ai didi

ios - UICollectionView 分页 - 如何获取最后一个可见单元格

转载 作者:行者123 更新时间:2023-11-29 01:28:18 27 4
gpt4 key购买 nike

我有一个 UICollectionView,其中每行包含 2 个单元格。现在,我需要启用分页,当 UICollectionView 的最后一个单元格出现时,我需要获取另一组记录。我怎样才能完成这件事。

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

MyCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cc" forIndexPath:indexPath];

if (indexPath.row == [mydictionary count]) {
pg = pg +1;

[self loadtheservice:pg];

}


return cell;
}

根据我上面的代码,[self loadtheservice:pg] 被调用了很多次。我认为原因是最终单元格 可见 很长时间并且 cellForItemAtIndexPath 正在调用加载服务方法。我该如何解决这个问题?

最佳答案

您可以使用 willDisplayCell 方法来检查 iOS8 可用的条件

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath

在 IOS8 下:一般我们用下面的。是的,它会调用每个滚动端,因此它可能不是首选。

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

因此,如果您不想这样,您可以使用 UICollectionViewDelegateFlowLayout 委托(delegate)来管理它。我正在给出粗略的想法。您可以根据您的要求实现。使用以下内容,

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
if(paging does not complete ) {
// you can give some nice gui to load more to notify user. if you dont want to notify user, just return with 1px height size
} else {
return zero size
}
}

您可以通过以下代表管理该 View

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
if(kind == UICollectionElementKindSectionFooter) {
// here you can add your nice load more functionality with some custom view . if you dont need load more view, you can add 1px height footer view and manage that if paging completed
}
}

关于ios - UICollectionView 分页 - 如何获取最后一个可见单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33759708/

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