Load More data on Scroll Demand on CollectionView

后端 未结 3 2065
臣服心动
臣服心动 2020-12-29 14:52

I am implementing a feature on the collectionView where user scrolls the collectionView at the bottom (20 items) and it requests another set of data (20 more items) from the

相关标签:
3条回答
  • 2020-12-29 15:15

    I do this thing through CollectionViewDelegate method like:

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

    In 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.
    
    }
    
    0 讨论(0)
  • 2020-12-29 15:19

    Swift 3 : CollectionView Delegate method

    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.
    }
    
    0 讨论(0)
  • 2020-12-29 15:20

    Swift 4.2

    When you call reloadData() inside CollectionView delegate method you will see blank cells. You need call it like this.

    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)
    }
    
    0 讨论(0)
提交回复
热议问题