views:

44

answers:

1

So I'm setting an activity indicator as the accessory view on my table view cells when they are selected.

UITableViewCell *cell = [tableVw cellForRowAtIndexPath:indexPath];
UIActivityIndicatorView *act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[act startAnimating];
[cell setAccessoryView:act];
[act release];

Later on, when I have the data I need to move on, I want to remove this activity indicator before I transition to another view so that the cell returns to it's default state. The following just isn't working.

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:self.tableView.indexPathForSelectedRow];
[cell.accessoryView removeFromSuperview];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
A: 

You should have some sort of way of determining whether or not an object requires an activity indicator in your tableView:cellForRowAtIndexPath:. For example, maintain a dictionary or array that stores whether or not a cell at an index path is still doing something, or create a model object with a property that represents whether or not it's loading, etc.

In your tableView:cellForRowAtIndexPath:, check if the cell at that index path needs an activity indicator view or not, and if it doesn't, set the cell's accessoryView to nil.

Then to remove the accessory view later: [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:theIndexPath] withRowAnimation:UITableViewRowAnimationNone];

anshuchimala
looks like they are trying to start the activity indicator on touch, then before the next view appears get rid of it, so starting the indicator in cellForRowAtIndexPath doesn't seem logical.
Jesse Naugher
Correct, the cell doesn't need an activity view as accessory until that row has been selected. I fetch some data in the background and when it's returned I want the cell to go back to normal.
E-Madd
No, you need to set the cell up in `cellForRowAtIndexPath:` (you don't necessarily have to start the activity indicator then, you can start it in `didSelectRowAtIndexPath:` if desired). Otherwise, if the table view reloads the cell (if it gets scrolled offscreen and recycled, etc) you'll run into problems. You need to make changes in your model and have your view update to reflect those changes by reloading the rows at the changed index paths.
anshuchimala