Question

I'm writing an app that performs HTTP request with AFNetworking framework, recieved data get parsed and then placed into a table view

I've created a special cell with loader, which showed when request in process. This cell have only a .xib file, no custom class. I've added activity indicator inside this cell, and checked option "Animating" in the Attributes Inspector.

Also i've created BOOL ivar to display this cell.

BOOL isLoading;

Here is the function performed when "Search" button clicked:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    if ([searchBar.text length] > 0){

        isLoading = YES;
        [self.tableView reloadData];

        searchResults = [NSMutableArray arrayWithCapacity:10];

        NSURL *url = [self urlWithSearchText:searchBar.text];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];

        AFJSONRequestOperation *operation = [AFJSONRequestOperation
             JSONRequestOperationWithRequest:request
             success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

                 [self parseDictionary:JSON];
                 [searchResults sortUsingSelector:@selector(compareName:)];

                 isLoading = NO;
                 [self.tableView reloadData];
             }
             failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){

                 [self showNetworkError];
                 isLoading = NO;
                 [self.tableView reloadData];
             }];

        [queue addOperation:operation];
    }
}

And here's how i get that cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (isLoading) {
        return [tableView dequeueReusableCellWithIdentifier:LoadingCellIdentifier];
    } 
    /*other code */
}

When i perform my first search this cell appears with animating Activity indicator and everything okay, but all the other times this cell appears with indicator that do no spin and thats the problem. Why this happening? How do i make it spin all the other times?

here screenshot of the app http://monosnap.com/image/TRFK4cXEvYK2VPpteE1j5x0eB.png

Was it helpful?

Solution

When you stop animating and hide the cell, it is not de-allocated. This means that next time you load the cell it will take the same state as when it was hidden. You will need to call [activityView startAnimating]; after loading the cell.

OTHER TIPS

Once try like this,in tableView willDisplayCell:method hide your alert instead of after JSONRequestOperationWithRequest:sucess method like below,

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
//hide your alert,i think in your case isLoading = NO to hide alert so,
isLoading = NO;
}

hope it will helps you...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top