objective-c - 事件指示器仅显示在一个单元格中

标签 objective-c ios cocoa

我有一个 UITableView,其中充满了显示文件名的单元格,单元格还指示文件的上传/下载进度。移动文件时,其对应的单元格应在其附属 View 中显示一个 UIActivityIndi​​catorView。我有一个 UIActivityIndi​​catorView 设置并准备进入 viewDidLoad但是当我试图将它设置为多个单元格的附件 View 时,它只显示在一个单元格

-(void)viewDidLoad {

    [super viewDidLoad];

    activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [activityIndicator startAnimating];

}


//...code that detects file changes and calls fileChange


-(void)fileChange {

    for (int i = 0; i < [self.cloudNames count]; i++) {

        //detect whether file name in array is uploading, downloading, or doing nothing

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

        if (downloading) {

            cell.accessoryView = activityIndicator;
            //other changes here specific to downloads

        } else if (uploading) {

            cell.accessoryView = activityIndicator;
            //other changes here specific to uploads

        } else {

            cell.accessoryView = nil;

        }

    }

}

正如我所说,事件指示器只显示在一个单元格中,即使有多个单元格应该显示它也是如此

我不想在 fileChange 方法中设置 UIActivityIndi​​catorView(即使它有效),因为在上传/下载过程中多次调用此方法。如果调用该方法并在那里设置事件指示器,则在调用该方法时事件指示器会在所有表格 View 单元格中重置,从而导致动画出现故障和不流畅,并导致巨大的内存问题。

有什么想法吗?谢谢。

最佳答案

即使您正在为单元格设置事件指示器,您也只能为它设置一个实例变量。这样做的方法是为 tableView:cellForRowAtIndexPath:

中的每个单元格创建一个指示器

您可以为 UIActivityIndi​​catorView 设置一个标签,无论何时您想要访问它或获取它,您都可以获取单元格,并使用 [cellView viewWithTag:theTag]。不需要实例变量。

如果你想让事情变得更漂亮,你可以子类化 UITableViewCell 并在你的自定义单元格中做任何你想做的事情..

编辑:

要获取 View ,您可以分配给附件 View 并仅获取单元格 accessoryView:

 UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

 UIActivityIndicatorView *indicator = (UIActivityIndicatorView *) cell.accessoryView;

或者您可以将 UIActivityIndi​​catorView 添加到单元格的 contentView(这样您就可以将它放在任何您想要的地方,您有更多的灵 active ):

添加指标:

myIndicatorView.tag = 1;
[cell.contentView addSubview:myIndicatorView];

获取指标:

UIActivityIndicatorView *indicator = [cell.contentView viewWithTag:1];

希望对你有帮助

关于objective-c - 事件指示器仅显示在一个单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11378037/

相关文章:

ios - 缺少 iPad 4.0 模拟器

ios - 当我编译 `GLSL` 代码时,出现错误 'ERROR: 0:15: ' premature EOF' : syntax error syntax error'

ios - NSPredicate 获取低于指定值的最大值

ios - 如何水平滚动collectionview?

ios - 正确的 : returning valid data of nil?

objective-c - 如何根据背景中的位图检查鼠标在屏幕上的位置? ( cocoa /Objective-C )

swift - Mac 中的 NSTableview reloadDataForRowIndexes 听起来

objective-c - 如何在 XCode 中激活依赖项目(框架)中的断点

Objective-C NSMutableArray removeObjectAtIndex : crashes

objective-c - 为什么将 unsigned int 传递给 performSelector 会丢失位?