Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView Detecting Last Cell

How can I detect when a UITableView has been scrolled to the bottom so that the last cell is visible?

like image 775
Ward Avatar asked Jul 13 '10 20:07

Ward


3 Answers

Inside tableView:cellForRowAtIndexPath: or tableView:willDisplayCell:forRowAtIndexPath: like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ...

    NSInteger sectionsAmount = [tableView numberOfSections];
    NSInteger rowsAmount = [tableView numberOfRowsInSection:[indexPath section]];
    if ([indexPath section] == sectionsAmount - 1 && [indexPath row] == rowsAmount - 1) {
        // This is the last cell in the table
    }

    ...

}
like image 70
Michael Kessler Avatar answered Sep 28 '22 17:09

Michael Kessler


Implement the tableView:willDisplayCell:forRowAtIndexPath: method in your UITableViewDelegate and check to see if it's the last row.

like image 30
Art Gillespie Avatar answered Sep 28 '22 17:09

Art Gillespie


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger lastSectionIndex = [tableView numberOfSections] - 1;
    NSInteger lastRowIndex = [tableView numberOfRowsInSection:lastSectionIndex] - 1;
    if ((indexPath.section == lastSectionIndex) && (indexPath.row == lastRowIndex)) {
        // This is the last cell
    }
}
like image 22
samwize Avatar answered Sep 28 '22 17:09

samwize