Detecting Last Cell Of UITableView

Discussion in 'iOS Programming' started by erdinc27, Dec 30, 2016.

  1. erdinc27 macrumors regular

    Joined:
    Jul 20, 2011
    #1
    I parse JSON according to page number and display the response in an UITableView. I calculate the page number like this
    Code:
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
        
        if(self.orderListTable.contentOffset.y >= (self.orderListTable.contentSize.height - self.orderListTable.frame.size.height)) {
            NSInteger pageNumber = scrollView.contentOffset.y /  (scrollView.contentSize.height - scrollView.frame.size.height);
            NSLog(@"END %ld", (long)pageNumber);
            self.scrollPage = [NSString stringWithFormat:@"%ld", (long)pageNumber];
            if (pageNumber == 0) {
                [self.orders removeAllObjects];
                [self getTheOrderList:self.selectedOrderType page:self.scrollPage];
            } else {
                if (self.previousScrollPage == pageNumber) {
                    return;
                } else {
                    [self getTheOrderList:self.selectedOrderType page:self.scrollPage];
                }
            }
            
            self.previousScrollPage = pageNumber;
        }
    }
    But in this calculation sometimes i get wrong page number. When the page number increase then the calculation gives a wrong result. (e.g: page number 3 from 5. It skips the 4). Is there any different way to make a paging with UITableView
     
  2. Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #2
    Why not us the the cellWillDisplay delegate method and check if the indexPath == last item in your datasource.
     
  3. erdinc27 thread starter macrumors regular

    Joined:
    Jul 20, 2011
    #3
    Thank u for reply. I tried to do this way also but it triggers the method 3 or 4 times in a row. So i checked if tableview stopped scrolling but this time it never invokes the method inside willDisplayCell.
    Code:
    - (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        
        if (indexPath.row == self.orders.count - 1) {
            if (self.isDecelerated) {
                NSInteger counter = [self.scrollPage integerValue];
                counter += 1;
                self.scrollPage = [NSString stringWithFormat:@"%ld", (long) counter];
                [self getTheOrderList:self.selectedOrderType page:self.scrollPage];
                NSLog(@"DIPTEYIZ");
            }
        }
    }
    
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.isDecelerated = YES;
    }
    
    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
    {
        if (!decelerate) {
            self.isDecelerated = YES;
        }
    }
    
    What is wrong here ?
     
  4. Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #4
    What do you mean called 3 or 4 times? The method willDisplayCell is always called when the tableView scrolls. Is willDIsplayCell not getting called at all?? If that is the case, then it sounds like you haven't set the delegate parameter for the tableView.
     

Share This Page