Оживление обновления UITableView, выполнено успешно, но требует более подробной информации - PullRequest
0 голосов
/ 05 сентября 2011

Я хочу показать анимацию при добавлении ячеек в UITableView.

Вот что я реализовал (псевдокод)

[self.tableView beginUpdates];

// remove row exists
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:(rows exists)  withRowAnimation:UITableViewRowAnimationFade];

(chang data source here, for me, it's NSFetchedResultsController)

// insert new rows
[self.tableView insertRowsAtIndexPaths:(new rows) withRowAnimation:UITableViewRowAnimationFade];

[self.tableView endUpdates];

Этот код хорошо показывает анимацию, но у него есть небольшая проблема.

Ячейки показывают перемещение анимации из прямоугольника кадра (0, 0, 0, 0) в его фактическое положение, а не только затухание анимации.

Мне кажется, проблема в том, что начальный кадр ячеек (0, 0, 0, 0), поэтому я установил свойство начального кадра ячейки в cellForRowAtIndexPath, но он не работает.

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

Как показать только анимацию оттенка, без анимации движения ячейки?

1 Ответ

1 голос
/ 08 сентября 2011

Код не проверен, но идея должна работать:

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;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...