0
votes

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!

2
Are you deselecting the row, or is the row staying highlighted?Andy Obusek
@obuseme I have tried it a couple ways. I have tried it with the selectionStyle to be none, I have tried it with the deselectRowAtIndexPath called before the animation starts. Neither of them work. I tried to do in what you recommended in your answer, but that did not work either. If I try to combine what your answer was, with danielbeard's answer and do it on the main thread like that, I do not see it either. If I comment out the stop animating, and do what you said, and danielbeard's answer to start the animation, I do see the activity indicator when i pop back to the view that has cellCrystal
@obuseme but that's obviously not what I want since I want it to animate, and then stop animating by the time i return to that view. The long task is setting up the next view to show and i wanted to show an indicator for that task.Crystal

2 Answers

3
votes

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];       
 });
0
votes

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.