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

前端 未结 4 1836
隐瞒了意图╮
隐瞒了意图╮ 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.

提交回复
热议问题