我正在尝试向我的 UITableView 中的某些单元格添加事件指示器。我在方法 didSelectRowAtIndexpath 中使用
成功地做到了这一点 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 实例,我失去了对事件指示器的“最新”初始化之外的所有引用,因此这意味着我只能控制一个。
我需要在这里做什么(如果可能的话?)来保持对所有 actIndicators 的引用,这样我就可以开始为它们制作动画了? 或者我能否以某种方式使用 actindicator.tag 来控制某种形式的引用。
非常感谢您的帮助。
编辑:(源自答案)访问 tableView 中标记为 1 的所有事件指示器实例(仅限可见单元格)可以使用以下另一种方法:
for (UITableViewCell *cell in [self.tableView visibleCells]) {
UIActivityIndicatorView *actView = (UIActivityIndicatorView *)[cell.contentView
viewWithTag:1];
[actView startAnimating];
activityFlag = 1;
}
上述方法将循环遍历所有可见单元格并开始为事件指示器设置动画。
为了处理 tableview 被滚动的情况,我使用下面位于 cellForRowAtIndexPath 中的方法重新设置指示器的动画。 cellStateKey 只是指示单元格旁边是否有一个复选标记,如果它确实有一个复选标记并且我的 activityflag(正在进行的异步网络服务器调用)已设置......那么我想继续制作动画。(技术上重新启动动画,作为滚动 tableview停止它)
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 方法中初始化了我的事件指示器(如果需要,也删除了事件指示器)
最佳答案
您可以将 UIActivityIndicatorView 添加为单元格的 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];
}
关于iphone - 如何将 UIActivity Indicator 添加到每个 Cell 并保持对每个单独指示器的控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16421673/