I am trying to change the accessor in a cell during some data loading : when the user selects the cell, I load data from a JSON service, and during this load I want to display a spinner as accessor. When loaded, the next view controller display what needed. I use this code but the accessor is not changed (even if I wait in the thread before returning from the method):
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIActivityIndicatorView *activityView =
[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[activityView startAnimating];
UIView *oldAccessorView = [cell accessoryView];
[cell setAccessoryView:activityView];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject: indexPath] withRowAnimation:UITableViewRowAnimationFade];
MembersViewController *membersViewController = [[MembersViewController alloc] init];
membersViewController.title = @"Members";
// load the members, must be done in a separate thread and must change the cell accessor type to a spinner...
JSONLoader *loader = [[JSONLoader alloc] init];
NSString *membersURL = [NSString stringWithFormat:@"%@/members/all.json", jug.apiURL];
NSMutableArray *members = [loader getMembersFromURL:membersURL];
membersViewController.members = members;
[loader release];
[members release];
[self.navigationController pushViewController:membersViewController animated:YES];
[membersViewController release];
// restore old accessor view
[NSThread sleepForTimeInterval:5];
[activityView stopAnimating];
[activityView release];
[cell setAccessoryView:oldAccessorView];
Not 100% sure I’m clear on the question. But the following might help:
1) It’s not clear from this code that you are giving iOS the opportunity to update the display. It only does that at the end of the run loop. But from what I can make out all your code is running in the same pass as the run loop. If that diagnosis is right, then some judicious use of
[self performSelector: @selector(method:) withObject: nil afterDelay: 0.0]will be needed.This question might be helpful background. The answer to a different questions illustrates how to deploy the performSelector:withObject:afterDelay: technique.
2) Unless I’m missing something, you need to add
[oldAccessoryView retain]before your[cell setAccessoryView: activityView]call. Then release it again after the subsequent `[cell setAccessoryView:oldAccessoryView]’. But maybe you are retaining the accessory view elsewhere.