skip to Main Content

how to make paging(load more data like facebook) in UITableView with call api?
Code:-

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat height = scrollView.frame.size.height;

    CGFloat contentYoffset = scrollView.contentOffset.y;

    CGFloat distanceFromBottom = scrollView.contentSize.height - contentYoffset;

    if(distanceFromBottom < height)
    {

        [self fetchNewsFeeds:[filteredArray count]+1 withLimit:20];

        NSLog(@"end of the table");
    }
}

scroll upto the content height after not call api

4

Answers


  1. Store response data in a NSArray and then add to a NSMutableArray

    NSArray *resObj = [NSArray alloc] init];
    NSMutableArray *aMutArray = [NSMutableArray alloc] init];
    [aMutArray addObject: resObj];
    

    //check for last index of tableview
    write this code in cellForItemAtIndexPath

     if(lastIndex)
        {
             // show load more button
        }
    
    Login or Signup to reply.
  2. Take Two Sections : The rows in the first section contains items which you want to display and the second section contains one row whose cell has a UIActivityIndicator.

    Then implement the given delegate method :

    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    
        if indexPath.section ==  1 {
    
            let footercell = cell as! ProductFooterCollectionViewCell
            footercell.loader.startAnimating()
            fetchSearchProducts()
         }             
    }
    
    Login or Signup to reply.
  3. Tableview Delegate when display cell this method call:

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSInteger totalRow = [tableView numberOfRowsInSection:indexPath.section];//first get total rows in that section by current indexPath.
        if(indexPath.row == totalRow-1)
        {
            // Code...
        }
    }
    

    Hope your problem solve.

    Login or Signup to reply.
  4. You need to first find last row of your tableviewcell.

    Below is the code for finding last row:

        - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
        {
            NSInteger totalNumberOfRows = [tableView numberOfRowsInSection:indexPath.section];
    
    // isLoadMore flage is TRUE when there is more data available.
            if(indexPath.row == totalNumberOfRows-1 && isLoadMore)
            {
                // Code...
                // Here you need to check if there are some load more data is there from pervious web-service or not.
        // If there is load more then call web-service and another set of data.
            }
        }
    

    You can use AFNetworking to get call your web-service.

    Hope you get logic 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search