我有一个表视图,在该表视图中,我有一个UIActivityIndicator,每个单元格中有一个按钮.现在点击那个按钮我想启动ActivityIndicator动画,它的开始.但问题是当我滚动表视图停止动画.这是我的代码为cellForRowAtIndexPath
- - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"txDevicesListCellID"];
- if (cell == nil) {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"txDevicesListCellID"];
- }
- UIButton *buttonutton = (UIButton *)[cell viewWithTag:103];
- UIActivityIndicatorView *activityIndicator = (UIActivityIndicatorView *) [cell viewWithTag:104];
- button.tag = indexPath.row;
- return cell;
- }
和我的按钮的选择器方法是
- -(IBAction)recevierButtonClick:(id)sender{
- UIButton *button = (UIButton *)sender;
- NSInteger index = button.tag;
- NSIndexPath* indexpath = [NSIndexPath indexPathForRow:serialNumber inSection:0];
- UITableViewCell* cell = [self.myTableView cellForRowAtIndexPath:indexpath];
- activityIndicator = (UIActivityIndicatorView*)[cell viewWithTag:index];
- [activityIndicator startAnimating];
- }
解决方法
我想我可以看出,纺纱商何时何地停止纺纱.
我将UIActivityIndicatorView子类化到我自己的类中,重载了startAnimating和stopAnimating函数,并在其中插入了断点.我已经做了一个简单的单元格,只有一个微调器上我的课.我已经将微调框的动画属性设置为IB:
我将UIActivityIndicatorView子类化到我自己的类中,重载了startAnimating和stopAnimating函数,并在其中插入了断点.我已经做了一个简单的单元格,只有一个微调器上我的课.我已经将微调框的动画属性设置为IB:
现在这里发生了什么.前两个屏幕截图有一个堆栈跟踪,指示两个动作(停止动画和启动它们)一个接一个地发生,在同一个专用函数_didMoveFromWindow:toWindow:
在我看来,这是在细胞创建流程中发生的,所以首先它没有动画初始化,然后IB设置启动并启动动画.现在这是有趣的部分,当微调停止动画:
所以看起来,当细胞从屏幕上移除时,微调器不断旋转,并且当单元格准备通过专用函数_removeAllAnimations再次显示在屏幕上(prepareForReuse)时停止旋转,该函数似乎递归地迭代所有子视图.
问题在于,由于某些原因,UIKit的私有功能不会重新启用动画,并且startAnimating永远不会被调用.实际上,IMO禁用动画是真正的问题.
我提出的解决方案,并不完美,但显然是苹果对我们的期望,是将UITableViewCell子类化为包含spinners的单元格,并在prepareForReuse中重新启用它们:
- class SpinnerCell: UITableViewCell {
- @IBOutlet weak var spinner: UIActivityIndicatorView?
- override func prepareForReuse() {
- super.prepareForReuse()
- if let spinner = self.spinner {
- spinner.startAnimating()
- }
- }
- }
或在Obj-C:
- @interface SpinnerCell
- @property (weak) IBOutlet UIActivityIndicatorView *spinner;
- @end
- @implementation SpinnerCell
- - (void)prepareForReuse {
- [super prepareForReuse];
- [self.spinner startAnimating];
- }
- @end