Infinite Scroll on iOS with Swift
Asked Answered
Z

3

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!

Zealotry answered 29/9, 2014 at 17:36 Comment(1)
github.com/canopas/MarqueeScrollRelaxation
A
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.

Atalante answered 29/9, 2014 at 17:49 Comment(2)
This works! Here is the swift code for that. Thanks!Zealotry
I added an answer here that shows this in SwiftLymphocyte
F
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

Frigid answered 29/9, 2014 at 17:46 Comment(0)
P
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.

Paxton answered 29/9, 2014 at 17:55 Comment(2)
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. :)Zealotry
I haven't done much Swift yet, but I think you would use the same APIs in Swift as you would in Objective-C.Atalante

© 2022 - 2024 — McMap. All rights reserved.