简体   繁体   中英

How do I add a UIActivity Indicator to every Cell and maintain control of each individual indicator

I'm trying to add an activity indicator to certain cells in my UITableView. I do this successfully in the method didSelectRowAtIndexpath using

    CGRect CellFrame = CGRectMake(260, 10, 20, 20);
    actindicator = [[UIActivityIndicatorView alloc]initWithFrame:CellFrame];
    [actindicator setHidesWhenStopped:NO];
    [actindicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    actindicator.tag =1;
    [[cell contentView] addSubview:actindicator];

The catch is I need to control these multiple activity indicators from ANOTHER METHOD. I figured a property was a way to do this however by initialising a new instance of actIndicator every time, I loose reference's to all but the 'latest' init of the activity indicator thus meaning I can only control one.

What do i need to do here (if even possible?) to maintain reference to all the actIndicators so i can begin animating ALL of them? Or Can I somehow use the actindicator.tag to control some form of reference.

Many thanks for any help.

EDIT: (Derived from answer) to access all Instances of Activity indicator with a tag of 1 in tableView (visible cells only) can use below from another method:

for (UITableViewCell *cell in [self.tableView visibleCells]) {
    UIActivityIndicatorView *actView = (UIActivityIndicatorView *)[cell.contentView
    viewWithTag:1];
    [actView startAnimating];
    activityFlag = 1;

}

The method above will cycle through all visible cells and start animating the activity indicator.

To handle the case of the tableview being scrolled, I re-animate the indicators using the method below which is in cellForRowAtIndexPath. cellStateKey simply indicates if the cell has a checkmark next to it, if it does have a checkmark and my activityflag (async webserver call in progress)is set..then i want to continue animating.(technically re-start animation, as scrolling tableview stops it)

if ([[rowData objectForKey:cellStateKey] boolValue]) {

    cell.accessoryType = UITableViewCellAccessoryCheckmark;

      if(cell.accessoryType == UITableViewCellAccessoryCheckmark &&activityFlag ==1){

        for (UIView *sub in [[cell contentView] subviews]) {

            if (sub.tag == 1) {

                UIActivityIndicatorView *acView = (UIActivityIndicatorView *)
                [cell.contentView viewWithTag:1];
                [acView startAnimating];


            }

        }

As mentioned in origional question I initialise my activity indicators (and remove activity indicators aswell if required) in the method didSelectRowAtIndexPath

You can add UIActivityIndicatorView as cell's accessoryView.

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

    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    spinner.frame = CGRectMake(0, 0, 24, 24);
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryView = spinner;
    [spinner startAnimating];
    [spinner release];
}

A simple way to do this (assuming you're adding the indicator as in your code) is to first get a collection of the visible cells (or rows) in your table, by calling [tableView visibleCells] . Then iterate through the cells like this:

for (UITableViewCell *cell in [tableView visibleCells]) {
    for (UIView *sub in [cell subViews]) {
        if (sub.tag == 1) { // "1" is not a good choice for a tag here
            UIActivityIndicatorView *act = (UIActivityIndicatorView *)sub;
            [act startAnimating]; // or whatever the command to start animating is
            break;
        }
    }
}

There's more complexity for you to deal with: in your original code, you need to make sure you're not adding an additional activity indicator to a pre-existing cell each time cellForRowAtIndexPath is called, and you need to account for the situation where the user might scroll the table at a later point, exposing cells that do not have their activity indicator turned on.

You need to make a custom UITableViewCell by extending it. Then have a UIActivityIndicatorView as a member of that Cell. Then you can access it and control it on a cell by cell basis.

Assuming that activity view indicator tag is unique in the cell.contentView you can try something like:

UITableViewCell *cell = [tableview cellForRowAtIndexPath:indexpath];

UIActivityIndicatorView *acView = (UIActivityIndicatorView *)[cell.contentView viewWithTag:1];

//acView will be your activitivyIndicator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM