3

我有一个UITableView并且想要为将再次出现的行设置动画。我也想在动画之间切换,一些单元格应该得到UITableViewRowAnimationLeft,而其他UITableViewRowAnimationRight的。但我不知道如何用我的UITableViewController. 我尝试将以下代码行插入cellForRowAtIndexPath

[self.tableView beginUpdates];
NSArray *updatePath = [NSArray arrayWithObject:indexPath];
[self.tableView reloadRowsAtIndexPaths:updatePath 
                      withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];

不是在单元格中滑动,而是单元格的顺序发生了变化,或者其中一些单元格出现了两次。我还尝试在单元格创建后插入这些行。

if (cell == nil) {
...
} else {
    [self.tableView beginUpdates];
    NSArray *updatePath = [NSArray arrayWithObject:indexPath];
    [self.tableView reloadRowsAtIndexPaths:updatePath 
                          withRowAnimation:UITableViewRowAnimationLeft];
    [self.tableView endUpdates];
4

1 回答 1

8

一旦表格开始在屏幕上显示单元格的过程,我认为您不会成功重新加载行。reloadRowsAtIndexPath通常会导致cellForRowAtIndexPath被调用,所以我很惊讶你没有进入无限循环。相反,表格似乎进入了糟糕的状态。

我的建议是在这种情况下制作您自己的动画,在willDisplayCell. 你可以这样做:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (<should animate cell>) {
        CGFloat direction = <animate from right> ? 1 : -1;
        cell.transform = CGAffineTransformMakeTranslation(cell.bounds.size.width * direction, 0);
        [UIView animateWithDuration:0.25 animations:^{
            cell.transform = CGAffineTransformIdentity;
        }];
    }
}

您需要为“应该为单元格设置动画”提供逻辑 - 您可能不想在初始加载时为单元格设置动画。

于 2013-08-28T15:06:35.067 回答