8

I'm an iOS newbie and I would like to know how to detect when the user scrolls and reaches the bottom of an UITableView so I can load new data into the table.

I would also like to know where such a method should be implemented (the tableview's class or the view controller in which this tableview exists) Cheers!

Luca Boieru
  • 2,011
  • 4
  • 14
  • 15

3 Answers3

14

You can use -(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath to and check if the last cell will be shown in the TableView's data source.

ansible
  • 3,561
  • 2
  • 16
  • 29
  • 4
    This works! [Here](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:willDisplayCell:forRowAtIndexPath:) is the swift code for that. Thanks! – Luca Boieru Sep 29 '14 at 18:50
  • I added [an answer here](http://stackoverflow.com/a/42902171/882630) that shows this in Swift – lukkea Mar 20 '17 at 11:32
4

iOS' TableViewController takes care of that automatically. It asks its datasource only for the currently visible rows of the table (tableview.cellForRowAtIndexPath)

See the documentation for the UITableViewDatasource Protocol Reference

zisoft
  • 21,200
  • 10
  • 56
  • 72
3

If you've got a "known dataset" (as in, you don't need to make a network call to fetch new data), then like @zizoft said, it'll be handled automatically in tableview.cellForRowAtIndexPath

If, however, you've got an "unknown dataset", (as in, you'll need to pull down data from the internet), you'll need to do something a bit more interesting - @ansible's suggestion would be appropriate in that case.

bryanjclark
  • 5,978
  • 2
  • 31
  • 63
  • I have an unknown dataset, I have to fetch data from the server. Basically I want something like Facebook's News Feed where you scroll and when you reach the bottom of the News Feed a request is made and more "stories" are appended. I would need the Swift code, not the Objective-C code. :) – Luca Boieru Sep 29 '14 at 18:16
  • 1
    I haven't done much Swift yet, but I think you would use the same APIs in Swift as you would in Objective-C. – ansible Sep 29 '14 at 18:24