【发布时间】:2020-07-26 20:25:36
【问题描述】:
我在 collectionView 上实现了一项功能,其中用户滚动底部的 collectionView(20 个项目),它从服务器请求另一组数据(另外 20 个项目)。
我已经实现了以下两种方法。但我想知道哪种方法更好?还是有其他我不知道的更好的方法?
第一种方法是在cellForItemAtIndexPath中使用indexPath如下
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
if(collectionView == productCollectionView)
{
__weak ProductCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
// load more data
if(indexPath.row == numberOfItemsPerSection-1)
{
numberOfItemsPerSection += 20;
offset += 20;
[self loadFromURL];
}
// loading image and text happening here.
// it is not included here
return cell;
}
}
第二种方法是scrollViewDidScroll如下
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView.contentOffset.y == scrollView.contentSize.height - scrollView.frame.size.height && pElements.count == numberOfItemsPerSection) {
numberOfItemsPerSection += 20;
offset += 20;
[self loadFromURL];
}
}
【问题讨论】:
标签: ios objective-c uicollectionview