Crystal
Crystal

Reputation: 29448

UIActivityIndicatorView in a UITableViewCell, not getting shown on selection

I have an action that can take a second or two when a row is selected in a UITableViewCell. I want to give the user feedback when they select the cell that I'm doing something. Currently it just shows the tableviewcell highlight. I added a UIActivityIndicatorView to my view. I have it hidden by default. I try to do this in my didSelectRowAtIndexPath:

{
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
            cell.activityIndicator.hidden = NO;
            [cell.activityIndicator startAnimating];

// do long task

            [cell.activityIndicator stopAnimating];
            cell.activityIndicator.hidden = YES;
}

This code does not show my activityindicator. If I delete the

activityIndicator.hidden = YES;

in the

setCustomObject:(id)newObject

of my CustomCell class, I do see the indicator. It's just static though. I want to hide it until they click on the cell, animate while long task is running, then stop animating and hide again when long task is over. Any thoughts? Thanks!

Upvotes: 0

Views: 2365

Answers (2)

danielbeard
danielbeard

Reputation: 9149

Try updating the activity indicator in the main thread

dispatch_async(dispatch_get_main_queue(), ^{
    cell.activityIndicator.hidden = NO;
    [cell.activityIndicator startAnimating];       
 });

 //do long task

 dispatch_async(dispatch_get_main_queue(), ^{
    cell.activityIndicator.hidden = YES;
    [cell.activityIndicator stopAnimating];       
 });

Upvotes: 3

Andy Obusek
Andy Obusek

Reputation: 12832

in the setCustomObject:(id)newObject method, instead of setting it to hidden, try this:

activityIndicator.hidesWhenStopped = YES;
[acitivtyIndicator stopAnimating];

Then in the didSelectRowAtIndexPath method, remove the code that sets "hidden" or not, and just use [activityIndicator startAnimating] or [activityIndicator stopAnimating] to control both animation and whether it's hidden or not.

Upvotes: 0

Related Questions