在CollectionView上的Scroll Demand上加载更多数据

时间:2017-01-28 00:44:59

标签: ios objective-c uicollectionview

我在collectionView上实现了一个功能,用户在底部滚动collectionView(20个项目),然后从服务器请求另一组数据(另外20个项目)。

我实施了以下两种方法。但我想知道哪种方法更好?还是有其他更好的方法,我不知道?

第一种方法是使用indexPath中的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;
   }
}

第二种方法是 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];
    }
}

3 个答案:

答案 0 :(得分:14)

我通过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 then 

    self.dataSoruce.append(newData);
    self.tableView.reloadData()
    self.isWating = false // it’s means paging is done and user can able request another page request by scrolling the tableview at the bottom.

}

答案 1 :(得分:7)

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

答案 2 :(得分:0)

Swift 4.2

reloadData()委托方法内调用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.
    DispatchQueue.main.async(execute: collectionView.reloadData)
}