如何将UIActivity指标添加到每个单元格并保持对每个指标的控制 [英] How do I add a UIActivity Indicator to every Cell and maintain control of each individual indicator

查看:65
本文介绍了如何将UIActivity指标添加到每个单元格并保持对每个指标的控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向UITableView中的某些单元格添加活动指示器.我在使用

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];

问题是我需要通过另一种方法来控制这些多个活动指标.我认为属性是一种实现此目的的方法,但是每次都初始化一个新的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.

在这里(如果可能的话)我需要做些什么来保持对所有actIndicator的引用,以便我可以开始为所有actIndicator设置动画? 或者我可以以某种方式使用actindicator.tag来控制某种形式的引用.

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.

非常感谢您的帮助.

(从答案中得出)以访问tableView中标记为1的所有Activity指示器实例(仅可见单元格)可以通过另一种方法在下面使用:

(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.

要处理表格视图被滚动的情况,我使用cellForRowAtIndexPath中的以下方法对指示器重新设置了动画. cellStateKey只是表明该单元格旁边是否有一个选中标记,是否确实有一个选中标记,并且是否设置了我的activityflag(正在进行异步网络服务器调用).然后我要继续进行动画制作(从技术上重新启动动画,如滚动tableview)停止它)

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];


            }

        }

如原始问题中所述,我在didSelectRowAtIndexPath方法中初始化了活动指标(并在需要时也删除了活动指标)

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

推荐答案

一种简单的方法(假设您正在代码中添加指标)是首先获取可见单元格(或行)的集合在表中,通过调用[tableView visibleCells].然后像这样遍历单元格:

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;
        }
    }
}

您需要处理的复杂性更高:在原始代码中,您需要确保没有在每次调用cellForRowAtIndexPath时都向现有单元格添加额外的活动指示器,并且需要考虑对于用户稍后可能会滚动表,暴露未打开活动指示器的单元格的情况.

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.

这篇关于如何将UIActivity指标添加到每个单元格并保持对每个指标的控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆