【问题标题】:Load More data on Scroll Demand on CollectionView在 CollectionView 上加载更多关于 Scroll Demand 的数据
【发布时间】: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


    【解决方案1】:

    我通过 CollectionViewDelegate 方法来做这件事,比如:

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

    在 Swift 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.
    
    }
    

    【讨论】:

    • 与我在问题中使用的其他方法(cellForItemAtIndexPathscrollViewDidScroll)相比,使用 willDisplayCell 方法有什么优势?
    • 它易于实现,并且该方法提供了当前单元格的索引路径,因此您可以在显示之前执行左侧单元格的操作。
    • 关于willDisplayCell 的优点:如果启用了预取,cellForItemAtIndexPath 可以在“必要”之前调用。在willDisplayCell 中进行 API 分页将带来最佳的时机和用户体验。反正就是这样。
    • 是否可以加载位于可见单元前面的单元?这些单元格将是动态大小的。
    • 在我看来我已经创建了一个循环。
    【解决方案2】:

    Swift 3: CollectionView 委托方法

    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
            if indexPath.row == numberofitem.count - 1 {  //numberofitem count
                updateNextSet()  
            }
    }
    
    func updateNextSet(){
           print("On Completetion")
           //requests another set of data (20 more items) from the server.
    }
    

    【讨论】:

      【解决方案3】:

      斯威夫特 4.2

      当您在 CollectionView 委托方法中调用 reloadData() 时,您将看到空白单元格。你需要这样称呼它。

      func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
          if indexPath.row == collectionView.numberOfItems(inSection: indexPath.section) - 1 {
              updateNextSet()
          }
      }
      
      func updateNextSet(){
          print("On Completetion")
          //requests another set of data (20 more items) from the server.
          DispatchQueue.main.async(execute: collectionView.reloadData)
      }
      

      【讨论】:

      • 这很好但是在加载后立即,如果没有更多数据要加载(数据结束),它将继续调用 updateNextSet()
      猜你喜欢
      • 1970-01-01
      • 2013-08-22
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-18
      • 1970-01-01
      • 2018-01-30
      相关资源
      最近更新 更多