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

前端 未结 4 1802
隐瞒了意图╮
隐瞒了意图╮ 2021-01-21 14:39

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

    CGRect CellFr         


        
相关标签:
4条回答
  • 2021-01-21 14:57

    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.

    0 讨论(0)
  • 2021-01-21 14:59

    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];
    }
    
    0 讨论(0)
  • 2021-01-21 15:03

    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.

    0 讨论(0)
  • 2021-01-21 15:06

    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
    
    0 讨论(0)
提交回复
热议问题