1
votes

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?

2
make cell.accessoryView = NULL; and then use your codeRamu Pasupuleti

2 Answers

2
votes

The accessoryView property has priority over the accessoryType property. If you set the accessoryView to nil, you'll see the accessory button again.

0
votes

Here's what I understood from the question: You'd like the accessory view to be a disclosure button that when tapped, becomes an activity indicator. After a short delay, you'd like to push to a new view controller.

Here's how I would do it:

  1. setup the prototype cell in storyboard to have Accessory:none.
  2. add a push segue from your view controller to the destination vc you want, give it an identifier, say @"MyPushSegue"

Add the accessoryView to cells (if they don't have one) in cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    // the rest of your cellForRowAtIndexPath

     if (!cell.accessoryView)
        cell.accessoryView = [self createAccessoryView];

    return cell;
}

Create the accessory view this way (assuming ARC):

- (UIView *)createAccessoryView {

    UIView *accessoryView = [[UIView alloc] initWithFrame:CGRectZero];
    UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    disclosureButton.tag = 100;
    [disclosureButton addTarget:self action:@selector(pressedAccessory:) forControlEvents:UIControlEventTouchUpInside];

    UIActivityIndicatorView *aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    aiv.tag = 101;
    aiv.alpha = 0.0;
    aiv.center = disclosureButton.center;

    accessoryView.bounds = disclosureButton.bounds;
    [accessoryView addSubview:aiv];
    [accessoryView addSubview:disclosureButton];
    return accessoryView;
}

The disclosure button in the above triggers a method when the button is pressed. This method should change the state of the selector view and trigger the push segue you setup in storyboard:

- (void)pressedAccessory:(UIButton *)sender {

    UIView *accessoryView = sender.superview;
    [self setAccessoryView:accessoryView waiting:YES];

    // let the user see the spinner for 1 seconds
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {
        [self performSegueWithIdentifier:@"MyPushSegue" sender:self];
        [self setAccessoryView:accessoryView waiting:NO];
    });
}

Finally, to solve the problem you were having, we don't replace the accessory view with a spinner, we change how it looks by hiding/showing the button and hiding/showing the spinner, depending on what state we want:

- (void)setAccessoryView:(UIView *)accessoryView waiting:(BOOL)waiting {

    UIButton *disclosureButton = (UIButton *)[accessoryView viewWithTag:100];
    UIActivityIndicatorView *aiv = (UIActivityIndicatorView *)[accessoryView viewWithTag:101];
    [UIView animateWithDuration:0.1 animations:^{
        disclosureButton.alpha = (waiting)? 0.0 : 1.0;
        aiv.alpha = (waiting)? 1.0 : 0.0;
    }];

    if (waiting) [aiv startAnimating]; else [aiv stopAnimating];
}