Ios 已成功设置UITableView更新的动画,但需要更多详细信息

Ios 已成功设置UITableView更新的动画,但需要更多详细信息,ios,uitableview,animation,Ios,Uitableview,Animation,我想在向UITableView添加单元格时显示动画 下面是我实现的(伪代码) 这段代码很好地显示了动画,但有一个小问题 单元格显示从帧矩形(0,0,0,0)移动到其实际位置的动画,而不仅仅是淡入淡出的动画 我认为问题在于单元格的初始帧是(0,0,0,0),所以我在CellForRowatineXpath中设置了单元格的初始帧属性,但它不起作用 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath

我想在向UITableView添加单元格时显示动画

下面是我实现的(伪代码)

这段代码很好地显示了动画,但有一个小问题

单元格显示从帧矩形(0,0,0,0)移动到其实际位置的动画,而不仅仅是淡入淡出的动画

我认为问题在于单元格的初始帧是(0,0,0,0),所以我在CellForRowatineXpath中设置了单元格的初始帧属性,但它不起作用

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ....
    cell.frame = CGRectMake(0, indexPath.row * 64, 320, 64);
    NSLog(@"set frame");
    ....
}

如何仅显示阴影信息,而不显示单元格移动动画?

代码未经测试,但该想法应该可行:

BOOL animateRowsAlpha = NO;

- (void)reloadData {
    [UIView animateWithDuration:0.2 
                     animations:^{
                         for (UITableViewCell *cell in self.tableView.visibleCells) {
                             cell.alpha = 0.0f;
                         }
                     } completion:^(BOOL finished) {
                         animateRowsAlpha = YES;
                         [self.tableView reloadData];
                     }
     ];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) 
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];

    if(animateRowsAlpha)
        cell.alpha = 0.0;

    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (!animateRowsAlpha) {
        return;
    }

    [UIView animateWithDuration:0.2 
                     animations:^{
                         cell.alpha = 1.0f;
                     }];

    NSArray *indexPaths = [tableView indexPathsForVisibleRows];
    NSIndexPath *lastIndexPath = [indexPaths lastObject];
    if(!lastIndexPath || [lastIndexPath compare:indexPath] == NSOrderedSame) {
        animateRowsAlpha = NO;
    }
}
BOOL animateRowsAlpha = NO;

- (void)reloadData {
    [UIView animateWithDuration:0.2 
                     animations:^{
                         for (UITableViewCell *cell in self.tableView.visibleCells) {
                             cell.alpha = 0.0f;
                         }
                     } completion:^(BOOL finished) {
                         animateRowsAlpha = YES;
                         [self.tableView reloadData];
                     }
     ];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) 
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];

    if(animateRowsAlpha)
        cell.alpha = 0.0;

    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (!animateRowsAlpha) {
        return;
    }

    [UIView animateWithDuration:0.2 
                     animations:^{
                         cell.alpha = 1.0f;
                     }];

    NSArray *indexPaths = [tableView indexPathsForVisibleRows];
    NSIndexPath *lastIndexPath = [indexPaths lastObject];
    if(!lastIndexPath || [lastIndexPath compare:indexPath] == NSOrderedSame) {
        animateRowsAlpha = NO;
    }
}