Trigger web-request when user scrolls to bottom of UITableView
-
18-09-2019 - |
Question
i'm trying to get a UIActivityIndicatorView to "appear and animate" when a user scrolls to the bottom of a UITableView….
Upon reaching the bottom of the table view, a web-request is made that will take a few seconds to complete and then the UIActivityIndicatorView should "stop and hide"
I'm triggering the appearance of the UIActivityIndicatorView using tableView:willDisplayCell:forRowAtIndexPath:
List item
But the problem is that the UIActivityIndicatorView doesn't appear on the iPhone screen until after the entire method (i.e., tableView:willDisplayCell:forRowAtIndexPath:) is called, and by then it's also already hidden.
Any ideas as to how to:
- show the indicator view
- make the web request
- hide the indicator view?
new thread? better way?
Thanks!!
specifically, my function looks like this
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.results && indexPath.row == [self.results count] - 1) {
[self.loadingSignal setHidden:NO]; // <====== my UIActivityIndicatorView is called "loadingSignal"
[self.loadingSignal startAnimating];
/// make web request that will take a few seconds
[self.loadingSignal setHidden:YES];
[self.loadingSignal stopAnimating];
}
}
}
Solution
Okay. From your above snippet of code, I see a few problems:
- You seem to plan on waiting for the request to finish before displaying a cell. This has a few negative points to it: it's annoying to the user and will most likely freeze the UI while the request goes out (which may take several seconds).
- Everything happens within the
willDisplayCell
method.
Here's how I think you should solve this problem:
- Have will display cell trigger loading asynchronously.
- Display a cell that says "Loading..." with your spinner on it (see Tweetie 2 for inspiration)
- Wait for the call-back from your request
- Populate your model with the response
- Remove the "Loading..." cell
- Force a reload of your data
[tableView reloadData]
This should allow you to achieve what you want to do.
OTHER TIPS
You can try doing so in the cellForRowAtIndexPath: method of the UITableView