关闭

animation tableview

710人阅读 评论(0) 收藏 举报
[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]; 

This code shows animation well, but it has a small problem.

The cells are shows moving animation from frame rect (0, 0, 0, 0) to it's actual position, not only fading animation.

I think problem is that initial frame of cells are (0, 0, 0, 0), so I set up initial frame property of cell in cellForRowAtIndexPath, but it does not work.

- (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; 
    } 
} 


 

Will above code work for deleting/inserting node into tableview
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:803249次
    • 积分:12271
    • 等级:
    • 排名:第1210名
    • 原创:321篇
    • 转载:658篇
    • 译文:30篇
    • 评论:44条
    友情链接
    友情链接 二
    最新评论