2011-09-05 69 views
0

我想在向UITableView添加單元格時顯示動畫。動畫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

的代碼是未經測試,但這個想法應該工作:

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; 
    } 
} 
+0

THX非常感謝!我會盡快嘗試代碼// – moon6pence