我正在 collectionView 上实现一项功能,其中用户滚动底部的 collectionView(20 个项目),并从服务器请求另一组数据(另外 20 个项目)。
我已经实施了以下两种方法。但我想知道哪种方法更好?或者还有其他我不知道的更好的方法吗?
第一种方法是使用indexPath
in the cellForItemAtIndexPath
如下
- (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;
}
}
第二种方法是滚动视图是否滚动如下
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView.contentOffset.y == scrollView.contentSize.height - scrollView.frame.size.height && pElements.count == numberOfItemsPerSection) {
numberOfItemsPerSection += 20;
offset += 20;
[self loadFromURL];
}
}
我通过 CollectionView Delegate 方法执行此操作,例如:
- (void)collectionView:(UICollectionView *)collectionView
willDisplayCell:(UICollectionViewCell *)cell
forItemAtIndexPath:(NSIndexPath *)indexPath{
if indexPath.row == numberOfitem.count-1 && !self.waiting {
waiting = true;
self.loadMoreData()
}
}
-(void)loadMoreData(){
// After getting the response then
[numberOfitem addObjects : your response in term of array];
waiting = false;
}
在迅捷4.0中
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
// Note: ‘isWating’ is used for checking the paging status Is currency process or not.
if indexPath.section == self.dataSoruce.list.count - 2 && !isWating {
isWating = true
self.pageNumber += 1
self.doPaging()
}
}
private func doPaging() {
// call the API in this block and after getting the response
self.dataSoruce.append(newData);
self.tableView.reloadData()
self.isWating = false // it means paging is done and the user can able request another page request via scrolling the table view at the bottom.
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)