I have a UITableView
that has cells with images, text and a disclosure button. I'm loading the cells like this:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
if ([indexPath row] == [[self multas] count]) {
[[cell textLabel] setText:@"Carregar mais..."];
}
else
{
Multa *multa = [self.multas objectAtIndex:indexPath.row];
NSString *dataIsolada = [[NSString stringWithFormat:[multa dataOcorrencia]] substringWithRange:NSMakeRange(0, 10)];
[[cell textLabel] setText:[NSString stringWithFormat:dataIsolada]];
[[cell detailTextLabel] setText:[multa descricao]];
[cell.imageView setImageWithURL:[NSURL URLWithString:[multa fotoURL]]
placeholderImage:[UIImage imageNamed:@"carregando.jpeg"]];
[cell.imageView setContentMode: UIViewContentModeScaleAspectFit];
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
}
return cell;
}
When the user taps the disclosure button, I switch the disclosure button for an Activity Indicator and then push another view to the screen using a navigation controller:
- (void)carregarDetalhesMulta:(UITableView *)tableView comMulta:(Multa *)multa
{
DetalheMultaViewController *form = [[DetalheMultaViewController alloc] initWithMulta:multa];
form.delegate = self;
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:form];
[self presentModalViewController:navigation animated:YES];
}
- (void) tableView: (UITableView *) tableView accessoryButtonTappedForRowWithIndexPath: (NSIndexPath *) indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleGray];
spinner.frame = CGRectMake(0, 0, 24, 24);
cell.accessoryView = spinner;
[spinner startAnimating];
Multa *multa = [self.multas objectAtIndex:indexPath.row];
double delayInSeconds = 0.1;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[spinner stopAnimating];
[self carregarDetalhesMulta:tableView comMulta:multa];
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
});
}
Note that I've changed back the accessory for a disclosure button in the end. The problem is: when I come back(by tapping the button back) to the view that contains the UITableView
, the line that I've tapped show no disclosure button nor Activity Indicator. What I am doing wrong? What's the better way to switch these controls without having to reload the table view?