views:

156

answers:

2

Hi, 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:

  1. show the indicator view
  2. make the web request
  3. 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];

        }
    }
}
A: 

Okay. From your above snippet of code, I see a few problems:

  1. 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).
  2. Everything happens within the willDisplayCell method.

Here's how I think you should solve this problem:

  1. Have will display cell trigger loading asynchronously.
  2. Display a cell that says "Loading..." with your spinner on it (see Tweetie 2 for inspiration)
  3. Wait for the call-back from your request
  4. Populate your model with the response
  5. Remove the "Loading..." cell
  6. Force a reload of your data [tableView reloadData]

This should allow you to achieve what you want to do.

Malaxeur
could you elaborate on step 1?
wonderful. this is working perfectly! thank you.
A: 

You can try doing so in the cellForRowAtIndexPath: method of the UITableView

MrThys