我有一个自定义单元格加载在我的集合视图的底部。它唯一的工作是显示活动指示器视图 - 这在应用程序进行新的工作调用时发生。
所以我将它添加到单元格中,如下所示:
BBLoaderCell *loaderCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LoaderCell" forIndexPath:indexPath];
UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityIndicator.hidesWhenStopped = YES;
activityIndicator.hidden = NO;
activityIndicator.center = loaderCell.imageView.center;
activityIndicator.tag = 10;
[loaderCell.imageView addSubview:activityIndicator];
[activityIndicator startAnimating];
return loaderCell;
这在我视图的最后一个单元格中显示了一个活动指示器 - 但它不旋转。有任何想法吗?
找到解决方案
根据评论 - 这是一个线程问题。我还采纳了将代码移动到单元格的自定义 nib 文件中的建议。
这是重构的代码:
BBLoaderCell *loaderCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LoaderCell" forIndexPath:indexPath];
loaderCell.backgroundColor = [UIColor clearColor];
if (self.loadingMore == NO){
dispatch_async(dispatch_get_main_queue(), ^{
activityIndicator.hidden = YES;
[loaderCell.spinner stopAnimating];
});
}
else if (self.loadingMore == YES){
dispatch_async(dispatch_get_main_queue(), ^{
activityIndicator.hidden = NO;
[loaderCell.spinner startAnimating];
});
}
return loaderCell;
这正在按我需要的方式工作。
多谢你们!
您最有可能致电reloadData
在很短的时间内不止一次。也许已经在下一帧了。默认情况下,活动指示器在停止时隐藏。发生的情况是这样的:
- 首先 reloadData 显示微调器
- 第二次 reloadData 也会重新加载单元格,并且微调器会隐藏自身
- 现在微调器已隐藏
- 您四处滚动,但单元格被重复使用。微调器仍然隐藏
您可以设置activityIndicator.hidesWhenStopped = false
。旋转器现在应该始终可见。但它不旋转。
添加此操作将恢复旋转。
override func prepareForReuse() {
activityIndicator.startAnimating()
}
另一个问题可能是颜色。也许微调器的颜色与背景相同。尝试像这样设置微调器的颜色:
activityIndicator.color = .red
You can check if the spinner is there by using the "Debug View Hierarchy" Button
It will show you the wireframe of the current visible screen
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)