我们的应用程序有一个表格视图,允许用户最小化/最大化其部分。还有一些动画与隐藏/显示行相关,具体取决于用户的输入,以及连接到不同的设备(通过蓝牙 4.0)。由于动画的不同来源,我们设置了一个调度程序来一次处理一个动画,这样我们就不会因为在 [tableview endUpdates] 之后不同步的行/部分的数量而发生任何崩溃。它对我们的目的非常有效。
但是,我们表中的一个新部分让我有些悲伤。根据连接到 iPad 的设备,该部分有 1 行或 8-9 行。其余行的高度为 0。当该部分有 8-9 行时,该部分最小化/最大化。但是,当该部分只有一行时,表格的 insertRowsAtIndexPaths 动画在该部分的行离开屏幕之前不会完成。因此,除非用户更改选项卡或向下滚动到足够远以将表格推离屏幕,否则由于我们的调度程序首先检查它是否已经忙,因此无法最小化/最大化其他部分。这是为动画调用的代码:
-(void)animateTableUsingAnimationType:(NSNumber*)aniType
{
static int animationID = -1;
if(tableAnimationBusy)
{
[self performSelector:@selector(animateTableUsingAnimationType:) withObject:aniType afterDelay:.05f];
}
else
{
tableAnimationBusy = true;
tat = [aniType intValue];
[CATransaction begin];
[CATransaction setCompletionBlock:^{
NSLog(@"Animation %d is complete!", tat);
tableAnimationBusy = false;
}];
//if-else if blocks checking for the animationID
//if open section 2
[self animateOpenTableSection:2]
else //undefined ID
tableAnimationBusy = false;
[CATransaction commit];
[self setUpLabelText];
}
}
animateOpenTableSection 是:
-(void)animateOpenTableSection:(NSInteger)section
{
NSLog(@"Calling open on section: %d", section);
if (section == 2)
{
[self.tableView beginUpdates];
openFlagSettingsRowCount = fsr_COUNT; //max row count for section
[[MCUISectionHandler sharedInstance] sectionTouched:YES forSection:MCUISH_SECTION_NAME__FLAG];
NSMutableArray *indexPathsToInsert = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < fsr_COUNT; i++) {
[indexPathsToInsert addObject:[NSIndexPath indexPathForRow:i inSection:section]];
}
[self.tableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}
}
如您所见,我在 animateOpenTableSection 函数中有调试语句,还有 animateTableUsingAnimationType 中的完成块。后者永远不会被调用,直到单行离开屏幕。关于为什么 tableview 不会放弃 CATransaction 的任何想法?