1

I got simple tableview and also every cell have got activity indicator but when i click button on row button alpha get 0 and indicator alpha get 1 like this

if (webserviceRunningValueInt == 1)
{
    cell.unBlockUserButtonOutlet.alpha = 0;
    cell.waitIndicator.alpha = 1;
    [cell.waitIndicator startAnimating];
}
else
{
     cell.waitIndicator.alpha = 0;
     [cell.waitIndicator stopAnimating];
     cell.unBlockUserButtonOutlet.alpha = 1;
}

this code part work but activity indicator start animation on all row :) how can i set this process only 1 row

Gökhan Çokkeçeci
  • 1,388
  • 3
  • 16
  • 37

1 Answers1

1

Hopefully I am understanding your question correctly: You want to click on a cell and have an activity indicator start animating?

I personally wouldn't bother with having an activity indicator on all of them and then hiding and revealing them, it makes it a bit of a pain to start and stop the correct indicators.

How I have done it in other apps is to add an indicator to the cell at the didSelectRow stage as we can know which cell we have clicked and immediately add a cell:

- (void)tableView:(UITableView *)tableView_ didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView_ reloadData];

    // This code will create and add an activity indicator
    UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    UITableViewCell * cell = [tableView_ cellForRowAtIndexPath:indexPath];

    cell.accessoryView = activityIndicator;
    [activityIndicator startAnimating];

    [tableView_ deselectRowAtIndexPath:indexPath animated:YES];
}

The neat thing about this code is that as we don't add the activity indicator when creating the cell means we can get rid of the indicator by refreshing the tableView. In this case each time we click we get rid of the previous indicator and add a new one.

Hopefully this helps a bit

EDIT: If you want this to occur from pressing a button in a cell (thanks to Vladimir for this answer)

This is pretty simple again and can use the code above.

Add a target to the button when creating the cell:

[button addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

This will enable us to find out when the button is tapped

- (void)checkButtonTapped:(id)sender {

    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];

    // Now we have the indexPath of the cell clicked so we can use the previous code again

    [tableView_ reloadData];

    UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    UITableViewCell * cell = [tableView_ cellForRowAtIndexPath:indexPath];

    cell.accessoryView = activityIndicator;
    [activityIndicator startAnimating];    
}
Community
  • 1
  • 1
sam_smith
  • 6,023
  • 3
  • 43
  • 60