我有一个 UITableView,其中充满了显示文件名的单元格,单元格还指示文件的上传/下载进度。移动文件时,其对应的单元格应在其附属 View 中显示一个 UIActivityIndicatorView
。我有一个 UIActivityIndicatorView
设置并准备进入 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
方法中设置 UIActivityIndicatorView
(即使它有效),因为在上传/下载过程中多次调用此方法。如果调用该方法并在那里设置事件指示器,则在调用该方法时事件指示器会在所有表格 View 单元格中重置,从而导致动画出现故障和不流畅,并导致巨大的内存问题。
有什么想法吗?谢谢。
最佳答案
即使您正在为单元格设置事件指示器,您也只能为它设置一个实例变量。这样做的方法是为 tableView:cellForRowAtIndexPath:
您可以为 UIActivityIndicatorView
设置一个标签,无论何时您想要访问它或获取它,您都可以获取单元格,并使用 [cellView viewWithTag:theTag]
。不需要实例变量。
如果你想让事情变得更漂亮,你可以子类化 UITableViewCell
并在你的自定义单元格中做任何你想做的事情..
编辑:
要获取 View ,您可以分配给附件 View 并仅获取单元格 accessoryView:
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UIActivityIndicatorView *indicator = (UIActivityIndicatorView *) cell.accessoryView;
或者您可以将 UIActivityIndicatorView 添加到单元格的 contentView(这样您就可以将它放在任何您想要的地方,您有更多的灵 active ):
添加指标:
myIndicatorView.tag = 1;
[cell.contentView addSubview:myIndicatorView];
获取指标:
UIActivityIndicatorView *indicator = [cell.contentView viewWithTag:1];
希望对你有帮助
关于objective-c - 事件指示器仅显示在一个单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11378037/